Getting type of a property of a typescript class using keyof operator

Is this what you’re looking for?

type PropType<TObj, TProp extends keyof TObj> = TObj[TProp];

and get type of an object property by doing:

type MyPropType = PropType<ObjType, '<key>'>;

which is the same as the way of using Pick in typescript, and it can report compile error if there’s any invalid key passed in.

Updates

As @astoilkov suggested, a simpler alternative is PropType['key'].

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)