0

Is there any way to specify the type of object a keyof is pointing to?

So that, for example:

type MyType = { a: string, b: number };
let myKey: keyof<string> MyType = "a"; // Valid
myKey = "b"; // Invalid

And then, when you use:

let myVar: MyType = { a: "Hello", b: 1 };
funcThatNeedsAString(MyType[myKey]);

it already knows that MyType[myKey] is a string?

Ken White
  • 123,280
  • 14
  • 225
  • 444
Trevortni
  • 688
  • 8
  • 23

0 Answers0