Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- type Attributes = {
- [key: string]: number;
- };
- function Fn<KeysOfAttributes extends string>(opts: { attributes: Attributes }): any {
- // ...
- }
- // variant 1
- const attributes = { // this object is hard coded (not dynamically generated)
- foo: 1,
- bar: 2,
- baz: 3
- };
- type Type = typeof attributes;
- type Keys = keyof Type;
- Fn<Keys>({
- attributes
- });
- // variant 2
- const attributes: Attributes = {
- foo: 1,
- bar: 2,
- baz: 3
- };
- type Type = typeof attributes;// equals {[key: string]: number;}
- type Keys = keyof Type;// equals string | number. Why ?
- Fn<Keys>({// Here, I would like Keys to be "foo" | "bar" | "baz", instead I have string | number
- attributes
- });
- Type 'string | number' does not satisfy the constraint 'string'.
- Type 'number' is not assignable to type 'string'.ts(2344)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement