Is there a way to extract values from an array as specific types in TypeScript?
const chars = ['a','b','c'] as const
type TChars = typeof chars[number] // 'a'| 'b' | 'c'
I want to achieve the same behavior for methods and properties within a class
, like this:
class Test{
constructor(public chars:string[]){}
get(char:string){
return char
}
}
new Test(['a','b','c']).get('d') //error only allow a,b or c
This means that the get()
method should restrict input to the initial characters passed to the constructor
.
I am willing to modify the class signature if necessary.