Is there a way to get/use the actual property name in an Typescript type definition.
type Mapped = { [P:string]: P; }
Where Mapped["foo"]
is of type "foo"
and Mapped["bar"]
is of type "bar"
, not string
.
I tried different variations on [P:string]
, [P in keyof any]
, typeof P
, readonly
and as const
or type Mapped<Props extends keyof any> = { [P in Props]: P }
and everything I came up with along these lines, but P
is at best typed as string
, never as the actual property name.
What do I need this for?
To build upon. Typing Proxies that dynamically generate utility functions using the accessed property name.
Something like
type GetProperty = { [P in (keyof any)]: <T>(obj:T) => P extends keyof T ? T[P] : void; }
My issue is, that since the generated/returned values are dynamic, I don't have a type T
to keyof T
the names from.
Could you please provide an example of expected bahaviour?
var obj:Mapped = new Proxy(...); // where the properties of `obj` should be typed as var foo:"foo" = obj.foo; var bar:"bar" = obj.bar;
And obj.asdf1234
should be of type "asdf1234"
没有评论:
发表评论