有没有办法根据 Typescript 中的装饰器来改变函数参数类型?
上下文:
我有这个界面。
export type Bar<T = any> = {
foo: T
taz: string
}
在我的函数中,我有这样的东西。
class MyClass {
something(param: Bar) {
// do anything
}
}
const instance = new MyClass()
const foo = { a: 1, b: 2, c: 3 }
instance.something({ foo, taz: 'calling'})
因此,在 something
方法中,我将 foo
作为任何类型。我想要的是根据装饰器中传递的参数键入 foo
,如下所示。
class MyClass {
@inferrerParamType({ a: number, b: number, c: number })
something(param: Bar) {
// do anything
}
}
通过这种方式,智能感知识别函数内 foo
的类型。
我想这样做,因为我将根据通知的数据结构在 param.foo
内部装饰器中应用一些更改。