TypeScript项目开发:从JavaScript整合到独立Web应用搭建
1. 在TypeScript项目中处理JavaScript
在TypeScript项目里,若代码会被其他项目使用,可让编译器在生成纯JavaScript代码的同时生成声明文件,这能为其他TypeScript开发者保留类型信息,且项目仍可作为普通JavaScript使用。不过,当 allowJS 选项启用时,编译器不会生成声明文件。所以,要移除对 formatters.js 文件的依赖,使项目全为TypeScript代码。
操作步骤如下:
1. 在 src 文件夹添加 tsFormatters.ts 文件,并添加以下代码:
export function sizeFormatter(thing: string, count: number): void {
writeMessage(`The ${thing} has ${count} items`);
}
export function costFormatter(thing: string, cost: number | string): void {
if (typeof cost === "number") {
writeMessage(`The ${thing} costs $${cost.toFixed(2)}`);
} else {
writeMessage(`The ${thing} co
超级会员免费看
订阅专栏 解锁全文
866

被折叠的 条评论
为什么被折叠?



