Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

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

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

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!