How to force object to have a property named over a union type in Typescript?

Viewed 47

I have the following types defined

type MyUnionType = "propA" | "propB"
type MyType = {
  myProp: MyUnionType
}

type SomeOtherType = MyType & { ... /* other props*/ ... }

Now, I would like to enhance MyType with something along the lines of

type MyType = {
  myProp: MyUnionType,
  [key in MyUnionType]: any // pseudo code
}

so that I can use it like this

const obj: SomeOtherType = {
  myProp: "propA",
  propA: "some value" // if this is missing, compiler will error
  [...] // some other props
}

I played around it a little bit, searched the web, but I didn't come up with any solution.

EDIT: I just removed string from MyUnionType as requested

1 Answers

For this to work, you want MyType to be a union type equivalent to:

type MyType = {myProp: "propA", propA: any} | {myProp: "probB", propB: any}

This has the following desirable behavior:

const goodObjA: MyType = {
  myProp: "propA",
  propA: "some value" 
} // okay

const badObjA: MyType = { // error! Property propA is missing
  myProp: "propA",
}

const goodObjB: MyType = {
  myProp: "propB",
  propB: "some value" 
} // okay

const badObjB: MyType = {
  myProp: "propB",
  propA: "oops" // error, unexpected property
}

So the only question now is how to generate MyType programmatically from MyUnionType. Conceptually we want to take each member K in the MyUnionType union, and produce a new union of { myProp: K } & { [P in K]: any } (which has a myProp property of type K, and a K-keyed property of type any. That is, we want to distribute the type F<K> = { myProp: K} & { [P in K]: any } operation across the MyUnionType union.

This can be done a few ways, but my approach is to make what's called a distributive object type as coined in microsoft/TypeScript#47109. The idea is that you make a mapped type over each K in MyUnionType, and then immediately index into it with MyUnionType, producing the desired union:

type MyType = { [K in MyUnionType]:
  { myProp: K } & { [P in K]: any }
}[MyUnionType]

This evaluates to

type MyType = 
  ({ myProp: "propA"; } & { propA: any; }) | 
  ({ myProp: "propB"; } & { propB: any; });

which is equivalent to the type we want.

Playground link to code

Related