深入探索 TypeScript 与 JavaScript 的协同工作
在现代前端开发中,TypeScript 凭借其强大的类型系统,为开发者提供了更高效、更安全的代码编写体验。然而,在实际项目中,我们不可避免地会使用到 JavaScript 代码,包括自己编写的以及第三方库。本文将详细介绍如何在 TypeScript 项目中与 JavaScript 代码协同工作,包括使用类型声明文件、描述第三方 JavaScript 代码等内容。
1. 函数修改与输出
首先,我们来看一个用于格式化货币价值的函数 costFormatter :
/**
* Format something that has a money value
* @param { string } thing - the name of the item
* @param { number | string } cost - the value associated with the item
*/
export function costFormatter(thing, cost) {
if (typeof cost === "number") {
writeMessage(`The ${thing} costs $${cost.toFixed(2)}`, true);
} else {
writeMessage(`The ${thing} costs $${cost}`);
}
}
function writeMessage(message) {
超级会员免费看
订阅专栏 解锁全文
6万+

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



