题目描述
TypeScript中如何获取一个接口内定义函数的参数类型呢?
题目来源及自己的思路
想了下不知道怎么做。
相关代码
interface Page {
fill(value: string, options?: {
force?: boolean;
noWaitAfter?: boolean;
timeout?: number;
}): Promise<void>;
}
interface InputProps extends Parameters<Page['fill']> {
locator?: string
}
let a: InputProps = { value: 'a', locator: 'b' } // Error
报错信息:
Type '{ value: string; locator: string; }' is not assignable to type 'InputProps'.
Object literal may only specify known properties, but 'value' does not exist in type 'InputProps'. Did you mean to write 'values'?
你期待的结果是什么?实际看到的错误信息又是什么?
获取'fill'的参数类型.
Parameters<Page['fill']>
实际拿到的是一个tuple数组类型:貌似没法拿到函数的参数名,相关的讨论:https://github.com/Microsoft/...