# TypeScript — JavaScript with Syntax for Types > TypeScript is a superset of JavaScript that adds static types, interfaces, generics, and modern ES features, compiled down to plain JavaScript. Created by Anders Hejlsberg at Microsoft. Used by Airbnb, Slack, Asana, and a huge share of modern web projects. ## Install Save as a script file and run: ## Quick Use ```bash # Install globally npm i -g typescript # Or in a project npm i -D typescript npx tsc --init ``` Example: ```ts // user.ts interface User { id: number; email: string; role: "admin" | "user"; } function greet(user: User): string { return `Hello, ${user.email} (${user.role})`; } const me: User = { id: 1, email: "w@tokrepo.com", role: "admin" }; console.log(greet(me)); ``` Compile: ```bash npx tsc # Compile once npx tsc --watch # Watch mode npx tsc --noEmit # Type-check only ``` `tsconfig.json` essentials: ```json { "compilerOptions": { "target": "ES2022", "module": "ESNext", "moduleResolution": "bundler", "strict": true, "esModuleInterop": true, "skipLibCheck": true, "forceConsistentCasingInFileNames": true, "declaration": true, "outDir": "dist" }, "include": ["src/**/*"] } ``` ## Intro TypeScript is a typed superset of JavaScript that compiles to plain JavaScript. Designed by Anders Hejlsberg (also creator of C# and Turbo Pascal) at Microsoft. First released in 2012, now used in most large-scale web projects. TypeScript makes JavaScript scalable for teams and big codebases by catching type errors at compile time and providing world-class IDE autocomplete and refactoring. - **Repo**: https://github.com/microsoft/TypeScript - **Stars**: 108K+ - **License**: Apache 2.0 ## What TypeScript Does - **Static types** — interfaces, type aliases, unions, intersections - **Generics** — parameterized types for reusable code - **Type inference** — infer types from values and signatures - **Type narrowing** — refine types via checks (typeof, instanceof, discriminators) - **Utility types** — Partial, Required, Pick, Omit, Record, Awaited - **Declaration files** — `.d.ts` for typing existing JS libraries - **Module resolution** — classic, node, bundler strategies - **JSX support** — TSX for React, Vue, Solid - **Project references** — incremental builds across packages - **Strict mode** — strict null checks, no implicit any ## Architecture TSC compiler written in TypeScript itself. Parses source to AST, performs type checking using a structural type system, then emits JavaScript. Language Server (tsserver) powers IDE features via LSP. ## Self-Hosting Language tool, ships via npm. ## Key Features - Structural type system - Type inference - Generics - Discriminated unions - Utility types - Declaration files for JS libraries - JSX/TSX support - Project references - Incremental builds - Rich LSP integration ## Comparison | Language | Types | Compiles To | Runtime | |---|---|---|---| | TypeScript | Static + inference | JavaScript | Node/Browser/Bun/Deno | | Flow | Static | JavaScript | Same | | Hegel | Static | JavaScript | Same | | ReScript | Static | JavaScript | Same | | PureScript | Static | JavaScript | Same | | Elm | Static | JavaScript | Browser | ## 常见问题 FAQ **Q: 什么时候用 TypeScript?** A: 任何 JS 项目超过 1000 行、团队规模 > 1、长期维护。小脚本可以不用。 **Q: Strict 模式必须开吗?** A: 强烈建议。关掉严格模式等于放弃 TypeScript 的核心价值。可以用 `@ts-expect-error` 标注临时例外。 **Q: 编译慢?** A: v5+ 引入 project references 和增量构建。大项目用 `tsc -b` 构建模式。也可用 swc、esbuild 只做转换跳过类型检查(配合 `tsc --noEmit` 单独检查)。 ## 来源与致谢 Sources - Docs: https://www.typescriptlang.org/docs - GitHub: https://github.com/microsoft/TypeScript - License: Apache 2.0 --- Source: https://tokrepo.com/en/workflows/1b748b68-35fe-11f1-9bc6-00163e2b0d79 Author: Script Depot