显式模块边界类型
要求所有模块导出拥有完整类型声明。
拥有完整类型化的函数参数和返回值,清晰地定义了模块的输入和输出(称为模块边界)。这将使模块的任何用户非常清楚如何以类型安全的方式提供输入和处理输出。
无效示例
// Missing return type (e.g. void)
export function printDoc(doc: string, doubleSided: boolean) {
return;
}
// Missing argument type (e.g. `arg` is of type string)
export const arrowFn = (arg): string => `hello ${arg}`;
// Missing return type (e.g. boolean)
export function isValid() {
return true;
}
有效示例
// Typed input parameters and return value
export function printDoc(doc: string, doubleSided: boolean): void {
return;
}
// Input of type string and a return value of type string
export const arrowFn = (arg: string): string => `hello ${arg}`;
// Though lacking a return type, this is valid as it is not exported
function isValid() {
return true;
}