typescript – how can you express keyof T to be a subset of String?

typescript – how can you express keyof T to be a subset of String?

This is related to a change in the way keyof works in 2.9, until 2.9 keyof only returned string keys, from 2.9 onward it will return number and symbol keys. This is the reference for this

To only accept string keys you can use Extract<keyof T, string> as the type for the key parameter, or if you want to revert to the old behavior you can use the --keyofStringsOnly compiler flag

public equalTo(value: number | string | boolean | null, key?: Extract<keyof T, string>): IQuery<T>

You can also require keys in your type to be string-only:

key?: keyof T & string

typescript – how can you express keyof T to be a subset of String?

Leave a Reply

Your email address will not be published. Required fields are marked *