class Example {
alpha(props: { check: boolean }) {
// noop
}
beta(props:
Partial<Parameters<this['alpha']>[0]> // this fails
// Partial<Parameters<Example['alpha']>[0]> // this works
// Partial<{ check: boolean }> // this works
) {
// noop
}
useBeta(props:
{ check?: boolean | undefined }
) {
const { check } = props
return this.beta({ check })
}
}
我一直在使用this
来引用方法的输入参数,但是我发现它不起作用的情况,我想对其进行记录/对这种情况为什么不起作用有一些想法。
我只是将{check: boolean}
提取到某个接口并将其传递。无论如何,这不是一个问题。问题是为什么它不适用于this
。
您不能保证this
将成为Example
实例。您可以随时通过apply
或call
通过。
class Example {
beta(props: Partial<{ check: boolean }>) {
console.log(this);
}
}
const example = new Example();
example.beta({ check: true });
// Object { }
Example.prototype.beta.apply("not an Example object", [{ check: true }]);
// "not an Example object"