I am creating a custom Type that should pick a generic's T object keys Key which type of T[Key] is number.
The code works but I want to make the error message clearer.
type KeysOfTypeNumber<T> = { [Key in keyof T]: T[Key] extends number ? Key : never }[keyof T]
type Keys = {
currentPage: number
prevPage: number
searchParam: string
}
const passNumberKeys = <T>(numberTypeKeys: KeysOfTypeNumber<T>) => {
console.log(numberTypeKeys)
}
passNumberKeys<Keys>('currentPage') // OK
passNumberKeys<Keys>('searchParam') // Type '"searchParam"' is not assignable to type 'KeysOfTypeNumber<Keys>'
Is it possible to fix my custom type so it throws something at least like Type '"searchParam"' is not assignable to type 'Argument must be of type number inside the generic'.
Already tried to follow this example, but the error remains the same.
Type '"searchParam"' is not assignable to type '"currentPage" | "prevPage"'instead, like this? If so, I can write up an answer. If not, can you explain why you prefer the string literal error message?