I can’t figure out why this code is throwing an error. – Type ‘{}’ is not assignable to type ‘Keys<T>’.
type Keys<T extends string|symbol>={
[key in T]: string;
};
const foo = <T extends string|symbol>()=>{
const a:Keys<T> = {}
return a
}
Moreover, if you manually substitute the string or symbol type, no errors will be received. Just a warning that T is declared for a function but not used.
Example of working codes:
type Keys<T extends string|symbol>={
[key in T]: string;
};
const foo = <T extends string|symbol>()=>{
const a:Keys<string> = {}
return a
}
type Keys<T extends string|symbol>={
[key in T]: string;
};
const foo = <T extends string|symbol>()=>{
const a:Keys<symbol> = {}
return a
}
You can check the code here
I expected generic code to work fine