Issue
I'm hitting this wall since days now and I'm not sure anymore if I'm the problem or if typescript is broken...
I defined a generic class with the generic extending Record<string, string>:
class DbTable<ColumnDefinitions extends Record<string, string>> { /* ... */ }
At a later point I want to use the exact keys (e.g. 'id' | 'name' instead of just string), so I use the keyof operator on the generic, like this:
function selectFields(fields: (keyof ColumnDefinitions)[]) { /* ... * / }
But keyof ColumnDefinitions resolves to string | number | symbol instead of just being of type string.
What did I miss?!
Here is a TS playground link with an example.
Solution
You can extract only strings from string | number | symbol in the method signature.
Instead of
fields: (keyof T)[]
you can write
fields: (Extract<keyof T, string>)[]
and the error goes away.
Answered By - Lazar Ljubenović
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.