I have a couple of inquiries regarding the code provided below:
- Can something other than an
enum
be passed as aParam
? - Is there a way to achieve more specific typing during execution of
e
that specifies the param key?
Ideally, both of these issues can be addressed using a unique symbol type such as:
new (class extends Symbol)('alpha')
enum Params {
alpha = 'alpha'
}
const defaultParam = <T> (param: Params) => {
return (a: { [param]: T }): T => {
throw new Error('need param')
}
}
const e = defaultParam<number>(Params.alpha)
e({ beta: 2 })
Below is the error message:
Argument of type '{ beta: number; }' is not assignable to parameter of type '{ [param]: number; }'.
Object literal may only specify known properties, and 'beta' does not exist in type '{ [param]: number; }'.
And with param: string
:
A computed property name in a type literal must refer to an expression whose type is a literal type or a 'unique symbol' type.