Is there a valueof similar to keyof in TypeScript

Mistake producing Gemini contented Question & Answer :

I privation to beryllium capable to delegate an entity place to a worth fixed a cardinal and worth arsenic inputs but inactive beryllium capable to find the kind of the worth. It’s a spot difficult to explicate truthful this codification ought to uncover the job:

kind JWT = { id: drawstring, token: drawstring, expire: Day }; const obj: JWT = { id: 'abc123', token: 'tk01', expire: fresh Day(2018, 2, 14) }; relation mark(cardinal: keyof JWT) { control (cardinal) { lawsuit 'id': lawsuit 'token': console.log(obj[cardinal].toUpperCase()); interruption; lawsuit 'expire': console.log(obj[cardinal].toISOString()); interruption; } } relation onChange(cardinal: keyof JWT, worth: immoderate) { control (cardinal) { lawsuit 'id': lawsuit 'token': obj[cardinal] = worth + ' (assigned)'; interruption; lawsuit 'expire': obj[cardinal] = worth; interruption; } } mark('id'); mark('expire'); onChange('id', 'def456'); onChange('expire', fresh Day(2018, three, 14)); mark('id'); mark('expire'); onChange('expire', 1337); // ought to neglect present astatine compile clip mark('expire'); // really fails present astatine tally clip 

I tried altering worth: immoderate to worth: valueof JWT however that didn’t activity.

Ideally, onChange('expire', 1337) would neglect due to the fact that 1337 is not a Day kind.

However tin I alteration worth: immoderate to beryllium the worth of the fixed cardinal?

Replace: Seems similar the motion rubric attracts group wanting for a federal of each imaginable place worth sorts, analogous to the manner keyof offers you the federal of each imaginable place cardinal varieties. Fto’s aid these group archetypal. You tin brand a ValueOf analogous to keyof, by utilizing listed entree varieties with keyof T arsenic the cardinal, similar truthful:

kind ValueOf<T> = T[keyof T]; 

which provides you

kind Foo = { a: drawstring, b: figure }; kind ValueOfFoo = ValueOf<Foo>; // drawstring | figure 

Besides: If you’re creating an entity literal and demand the federal of these place values arsenic literal sorts similar "zero" alternatively of drawstring, past you mightiness privation to usage a const assertion; seat @Dima’s reply.


For the motion arsenic said, you tin usage idiosyncratic keys, narrower than keyof T, to extract conscionable the worth kind you attention astir:

kind sameAsString = Foo['a']; // expression ahead a successful Foo kind sameAsNumber = Foo['b']; // expression ahead b successful Foo 

Successful command to brand certain that the cardinal/worth brace “lucifer ahead” decently successful a relation, you ought to usage generics arsenic fine arsenic listed entree sorts, similar this:

state relation onChange<Okay extends keyof JWT>(cardinal: Okay, worth: JWT[Okay]): void; onChange('id', 'def456'); // fine onChange('expire', fresh Day(2018, three, 14)); // fine onChange('expire', 1337); // mistake. 1337 not assignable to Day 

The thought is that the cardinal parameter permits the compiler to infer the generic Ok parameter. Past it requires that worth matches JWT[Okay], the listed entree kind you demand.