3

If one creates dynamic properties based on parameter inputs:

type ObjBuilder<Prop1 extends string, Prop2 extends string> = {
  [P in Prop1 | Prop2]: P extends Prop2  ? ()=>boolean : ()=>string
}

function objectBuilder<Prop1 extends string, Prop2 extends string>(
  prop1: string,
  prop2: string,
) : ObjBuilder<Prop1, Prop2> {
  return {
    [prop1]: () => true,
    [prop2]: () => prop2,
  } as ObjBuilder<Prop1, Prop2>
}

// note the duplication of information in this code
const a = objectBuilder<'p1', 'p2'>('p1', 'p2')

console.log(a.p1()) // true
console.log(a.p2()) //'p2'

Is it possible to access the type definitions in the function definition to avoid the duplication of objectBuilder<'p1', 'p2'>('p1', 'p2') and rather only have objectBuilder<'p1', 'p2'>() with the compiled javascript having access to the strings p1 and p2?

In other words, have the JavaScript somehow via reflection access the type information and make it available at runtime?

code here

2
  • 1
    No, it is not possible. Try this. It is possible to infer arguments type so you don't need to explicitly use <'p1', 'p2'> Commented Jan 5, 2022 at 8:11
  • 1
    @captain-yossarian - thank you, yes that's a good solution as it also removes the duplication. Commented Jan 6, 2022 at 0:30

1 Answer 1

1

You can infer literal type of arguments.

COnsider this example:

type ObjBuilder<Prop1 extends string, Prop2 extends string> = {
  [P in Prop1 | Prop2]: P extends Prop2 ? () => boolean : () => string
}

function objectBuilder<Prop1 extends string, Prop2 extends string>(
  prop1: Prop1,
  prop2: Prop2,
): ObjBuilder<Prop1, Prop2> {
  return {
    [prop1]: () => true,
    [prop2]: () => prop2,
  } as ObjBuilder<Prop1, Prop2>
}

const a = objectBuilder('p1', 'p2')
console.log(a.p1())
console.log(a.p2())

Playground

As you might have noticed, I have used Prop1 and Prop2 generics as an argument type instead of string. In this way TS is able to infer literal type.

If you are interested in this topis (function arguments type inference) you can check my article

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.