显式模块边界类型
要求所有模块导出都具有完整的类型声明。
具有完整类型的功能参数和返回值清晰地定义了模块的输入和输出(称为模块边界)。这将使用户非常清楚地了解如何以类型安全的方式提供输入和处理输出。
无效
// 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;
}