Skip to content

TypeScript

一句话解释

TypeScript 是 strongly 输入 programming language 那个构建 on JavaScript 通过添加静态类型定义, catching 错误之前 code 运行.

用大白话说

TypeScript 是JavaScript with types。 It adds a static type system on top of JavaScript, letting you declare the types of variables, function parameters, and return values. The TypeScript compiler (tsc) checks that your code uses types correctly, then erases all type annotations and outputs plain JavaScript.

可以把它看作 a safety net: JavaScript lets you do anything at runtime, which can lead to bugs. TypeScript catches many of those bugs at compile time — before the code ever runs. For example, if you try to call a string method on a number, TypeScript tells you immediately.

TypeScript is a superset of JavaScript, meaning every valid JavaScript program is also a valid TypeScript program (with relaxed settings). This makes it easy to adopt incrementally — you can rename .js files to .ts and add types gradually.

示例

typescript
let count: number = 5;
let name: string = "Alice";

function add(a: number, b: number): number {
  return a + b;
}

console.log(add(count, 10)); // 15

类型 annotations (: 数字, : 字符串) 是检查 at 编译时间, 然后 erased. 输出是 plain JavaScript.

相关术语

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误

← 返回术语表