Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Typescript 类型脚本检查导出接口_Typescript - Fatal编程技术网

Typescript 类型脚本检查导出接口

Typescript 类型脚本检查导出接口,typescript,Typescript,如何对模块的导出符号进行打字检查 例如,考虑一个基于插件的系统,其中插件必须实现和导出预定义的API(例如函数原型或接口) 键入MyPluginCallback=(foo:string)=>number; 当实现插件时,如果插件没有实现给定的API,我希望有一个构建错误 /*正常*/ module.exports=函数myPlugin(foo:string):编号{ /* ... */ } /*错误,错误的原型*/ module.exports=函数myPlugin(条:编号):编号{ /*

如何对模块的导出符号进行打字检查

例如,考虑一个基于插件的系统,其中插件必须实现和导出预定义的API(例如函数原型或接口)

键入MyPluginCallback=(foo:string)=>number;
当实现插件时,如果插件没有实现给定的API,我希望有一个构建错误

/*正常*/
module.exports=函数myPlugin(foo:string):编号{
/* ... */
}
/*错误,错误的原型*/
module.exports=函数myPlugin(条:编号):编号{
/* ... */
}
可以用打字脚本吗?如果是,如何使用?

为此,您可以将
用作MyPluginCallback
as MyPluginCallback
在将类型
MyPluginCallback
分配给当前函数表达式之前,执行类型兼容性检查

typemyplugincallback=(foo:string)=>number
module.exports=函数myPlugin\u OK(条形图:字符串):编号{
返回42
}作为MyPluginCallback//✅
module.exports=函数myPlugin\u错误(条:编号):编号{
返回42

}正如MyPluginCallback//该插件是否来自您拥有的节点模块?如果是这样,请检查您可能会共享您的typescript配置以查看属性的值。当我实现插件时,我希望看到构建失败,例如,如果在更高版本的消费者项目中更改了API。