如何在typescript中键入参数为'any'或'void'的函数?

如何在typescript中键入参数为'any'或'void'的函数?,typescript,Typescript,我想键入一个函数,该函数在typescript中接受某些内容或不接受任何内容。我该怎么做 我试过: interface TestFn { (props: any | void): string } const thing: TestFn = (props) => 'whoo'; thing('something'); // this line is fine thing(); // this is not okay 您可以使用可选参数: interface TestFn {

我想键入一个函数,该函数在typescript中接受某些内容或不接受任何内容。我该怎么做

我试过:

interface TestFn {
    (props: any | void): string
}

const thing: TestFn = (props) => 'whoo';
thing('something'); // this line is fine
thing(); // this is not okay

您可以使用可选参数:

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
接口测试fn{
(道具?:任何):字符串//‘哇’;
thing('something');//这行没问题
thing();//这行也可以

参数
props
标记为
,这意味着该参数是可选的。您可以在可选和默认参数部分找到有关可选参数的更多信息。哦,我的代码中也有可选参数。谢谢您的帮助。我会尽快接受答案!