TypeScript - chunhualiao/public-docs GitHub Wiki
TypeScript is a programming language developed by Microsoft that extends JavaScript by adding static types. It’s a superset of JavaScript, meaning all valid JavaScript code is also valid TypeScript code, but TypeScript introduces optional type annotations and other features to enhance code quality and maintainability, especially in large-scale applications.
Feature/Aspect | TypeScript | JavaScript |
---|---|---|
Definition | A superset of JavaScript with static typing. | A dynamic, interpreted scripting language. |
Typing | Static typing with optional type annotations. | Dynamic typing, no compile-time type checks. |
Syntax | JavaScript + type annotations, interfaces, etc. | Standard ECMAScript syntax. |
Example | let x: number = 10; |
let x = 10; |
Compilation | Requires compilation to JavaScript via tsc . |
No compilation needed; runs directly. |
Error Detection | Catches type errors at compile time. | Errors often detected only at runtime. |
Tooling | Rich IDE support (autocompletion, refactoring). | Basic tooling, less type-aware support. |
Features | Enums, interfaces, generics, access modifiers. | No enums or interfaces; uses prototypes. |
Learning Curve | Steeper due to static types and new features. | Easier for beginners; simpler syntax. |
Performance | Same as JavaScript (compiles to JS). | Native runtime performance. |
Use Cases | Large-scale apps, frameworks (React, Angular). | General-purpose, web, and server-side (Node.js). |
Interoperability | Fully compatible with JavaScript libraries. | Native language; no additional setup needed. |
Type Safety | Strong type safety with compile-time checks. | No type safety; relies on runtime checks. |
Community/Support | Growing, with strong Microsoft backing. | Massive, mature ecosystem; standard for web. |
File Extension |
.ts or .tsx (for React). |
.js or .jsx (for React). |
Runtime Environment | Compiles to JavaScript; runs anywhere JS does. | Runs directly in browsers, Node.js, etc. |
-
Static Typing:
- Developers can define types for variables, function parameters, and return values (e.g.,
number
,string
,boolean
, or custom types). - Types are checked at compile time, catching errors like type mismatches before the code runs.
- Example:
let age: number = 30; let name: string = "Alice"; // Error: Type 'string' is not assignable to type 'number' // age = "thirty";
- Developers can define types for variables, function parameters, and return values (e.g.,
-
Interfaces and Type Aliases:
- TypeScript allows defining custom types using
interface
ortype
to describe object shapes, function signatures, or complex data structures. - Example:
interface User { id: number; name: string; } let user: User = { id: 1, name: "Bob" };
- TypeScript allows defining custom types using
-
Advanced Type Features:
- Supports features like union types (
string | number
), intersection types, generics, and type inference. - Example with generics:
function identity<T>(value: T): T { return value; } let num = identity<number>(42); // Type-safe generic function
- Supports features like union types (
-
Tooling and IDE Support:
- TypeScript provides rich tooling, including autocompletion, type checking, and refactoring support in editors like VS Code.
- It helps catch errors early through compile-time checks.
-
Compiles to JavaScript:
- TypeScript code is transpiled to plain JavaScript using the TypeScript compiler (
tsc
), making it compatible with any JavaScript environment (browsers, Node.js, etc.). - Developers can target specific ECMAScript versions (e.g., ES5, ES6) during compilation.
- TypeScript code is transpiled to plain JavaScript using the TypeScript compiler (
-
Enums and Other Features:
- TypeScript adds features like
enum
for defining sets of named constants and access modifiers (public
,private
,protected
) for classes. - Example:
enum Color { Red, Green, Blue } let c: Color = Color.Green;
- TypeScript adds features like
- Error Detection: Catches type-related errors during development, reducing runtime bugs.
- Scalability: Improves maintainability in large codebases by providing clear type contracts.
- Refactoring: Makes code refactoring safer and easier with type-aware tooling.
-
Interoperability: Works seamlessly with existing JavaScript libraries, with type definitions available via DefinitelyTyped or
@types
packages.
- Learning Curve: Developers new to static typing may need time to adapt.
- Build Step: Requires compilation to JavaScript, adding a step to the development workflow.
- Not Runtime-Enforced: Types are erased during compilation, so TypeScript’s type safety doesn’t apply at runtime.
- Widely used in web development, especially with frameworks like React, Angular, and Vue.js.
- Popular in large-scale projects (e.g., enterprise applications, Node.js backends) where type safety improves code reliability.
- Examples: VS Code, Slack, and parts of Microsoft’s own products are built with TypeScript.
Here’s a simple TypeScript function with type annotations:
function greet(person: { name: string; age: number }): string {
return `Hello, ${person.name}! You are ${person.age} years old.`;
}
console.log(greet({ name: "Alice", age: 25 }));
// Error: Property 'age' is missing
// console.log(greet({ name: "Bob" }));
- Install TypeScript:
npm install -g typescript
- Compile a
.ts
file:tsc file.ts
(outputsfile.js
) - Run the JavaScript output with Node.js or in a browser.
- TypeScript can be used in projects via
tsconfig.json
for configuration.
If you want to dive deeper into TypeScript’s features, set up a project, or explore how it integrates with SARIF for static analysis (e.g., type checking results), let me know!