我想键入一个函数,在typescript中输入或不包含任何内容。我该怎么做?
我试过了:
interface TestFn {
(props: any | void): string
}
const thing: TestFn = (props) => 'whoo';
thing('something'); // this line is fine
thing(); // this is not okay
答案 0 :(得分:2)
您可以使用可选参数:
interface TestFn {
(props?: any): string // <- parameters is marked as optional
}
const thing: TestFn = (props) => 'whoo';
thing('something'); // this line is fine
thing(); // this line is fine as well
参数props
标有?
,表示该参数是可选的。您可以在TypeScript documentation,可选和默认参数部分找到有关可选参数的详细信息。