I have a method like this:
public select(fieldName: keyof TType)
Where TType
could be an array type. In case of an array type
You will need a little helper to extract the boxed type:
type Unboxed =
T extends (infer U)[]
? U
: T;
Then your method can look like this:
interface User {
id: symbol;
name: string;
}
class Foo {
select(fieldName: keyof Unboxed) {
console.log(fieldName) // "id" | "name"
}
}
As to your extra question: yes, it's possible, but it may feel a bit strange.
class Foo {
select(fieldName: keyof Unboxed) {
console.log(fieldName)
}
}
new Foo()
.select('addEventListener')
Type parameters are meant to describe the arguments living inside the method or the generic type of the class. So perhaps you wanted to do the following:
class Foo {
select(fieldName: keyof Unboxed) {
console.log(fieldName)
}
}
new Foo()
.select('addEventListener')