-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Expand file tree
/
Copy pathtypescript.ts
More file actions
670 lines (553 loc) Β· 14.9 KB
/
typescript.ts
File metadata and controls
670 lines (553 loc) Β· 14.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
/****************************
* TYPESCRIPT CHEATSHEET - Quick Reference
* Learn more: https://www.typescriptlang.org/docs/
* Playground: https://www.typescriptlang.org/play
* Handbook: https://www.typescriptlang.org/handbook/
*
* Table of contents
* -------------------
* 01 | Basic Types
* 02 | Variables & Arrays
* 03 | Functions
* 04 | Objects & Interfaces
* 05 | Classes
* 06 | Generics
* 07 | Union & Literal Types
* 08 | Type Guards & Assertions
* 09 | Utility Types
* 10 | Enums
* 11 | Modules
* 12 | Advanced Types
* 13 | Decorators
* 14 | Configuration
* 15 | Common Patterns
*****************************/
/***************************
------------ 01: Basic Types -----------
*******************************/
// Primitive Types
let str: string = "hello";
let num: number = 42;
let bool: boolean = true;
let undef: undefined = undefined;
let nul: null = null;
// Special Types
let anything: any = "can be anything";
let unknown: unknown = "type-safe any";
let nothing: void = undefined;
let never: never = (() => { throw new Error() })();
// Type Inference
let auto = "TypeScript infers string";
let nums = [1, 2, 3]; // number[]
/***************************
------------ 02: Variables & Arrays -----------
*******************************/
// Arrays
let numbers: number[] = [1, 2, 3];
let strings: Array<string> = ["a", "b"];
let mixed: (string | number)[] = [1, "two"];
// Tuples
let tuple: [string, number] = ["hello", 42];
let namedTuple: [name: string, age: number] = ["John", 30];
// Destructuring
let [first, second] = tuple;
let [x, y, ...rest] = [1, 2, 3, 4, 5];
// Object Destructuring
let {name, age} = {name: "John", age: 30};
let {a: newName, b = 10} = {a: "value"}; // rename & default
/***************************
------------ 03: Functions -----------
*******************************/
// Function Declaration
function add(x: number, y: number): number {
return x + y;
}
// Arrow Functions
const multiply = (x: number, y: number): number => x * y;
const greet = (name: string): void => console.log(`Hello ${name}`);
// Optional & Default Parameters
function build(first: string, last?: string, age = 25): string {
return `${first} ${last || ""} (${age})`;
}
// Rest Parameters
function sum(...nums: number[]): number {
return nums.reduce((a, b) => a + b, 0);
}
// Function Overloads
function format(x: string): string;
function format(x: number): string;
function format(x: string | number): string {
return x.toString();
}
// Function Types
type MathOp = (x: number, y: number) => number;
const divide: MathOp = (x, y) => x / y;
/***************************
------------ 04: Objects & Interfaces -----------
*******************************/
// Object Types
let person: {name: string, age: number} = {name: "John", age: 30};
// Interface
interface User {
readonly id: number;
name: string;
email?: string; // optional
[key: string]: any; // index signature
}
// Extending Interfaces
interface Admin extends User {
permissions: string[];
}
// Multiple Inheritance
interface Timestamped {
createdAt: Date;
}
interface AdminUser extends User, Timestamped {
role: "admin";
}
// Function in Interface
interface Calculator {
add(x: number, y: number): number;
subtract: (x: number, y: number) => number;
}
/***************************
------------ 05: Classes -----------
*******************************/
// Basic Class
class Animal {
public name: string;
private age: number;
protected species: string;
readonly id: number;
constructor(name: string, age: number) {
this.name = name;
this.age = age;
this.species = "unknown";
this.id = Math.random();
}
speak(): void {
console.log(`${this.name} makes a sound`);
}
}
// Inheritance
class Dog extends Animal {
breed: string;
constructor(name: string, age: number, breed: string) {
super(name, age);
this.breed = breed;
}
speak(): void {
console.log(`${this.name} barks`);
}
}
// Abstract Class
abstract class Shape {
abstract area(): number;
display(): void {
console.log(`Area: ${this.area()}`);
}
}
// Static Members
class MathUtils {
static PI = 3.14159;
static circle(radius: number): number {
return 2 * MathUtils.PI * radius;
}
}
// Getters/Setters
class Person {
private _age: number = 0;
get age(): number {
return this._age;
}
set age(value: number) {
if (value >= 0) this._age = value;
}
}
/***************************
------------ 06: Generics -----------
*******************************/
// Generic Functions
function identity<T>(arg: T): T { return arg; }
const result = identity<string>("hello");
const inferred = identity(42); // T inferred as number
// Multiple Type Parameters
function pair<T, U>(first: T, second: U): [T, U] {
return [first, second];
}
// Generic Interface
interface Container<T> {
value: T;
getValue(): T;
}
// Generic Class
class Box<T> {
contents: T;
constructor(value: T) {
this.contents = value;
}
}
// Constraints
interface HasLength {
length: number;
}
function logLength<T extends HasLength>(arg: T): void {
console.log(arg.length);
}
// Keyof Constraint
function getProperty<T, K extends keyof T>(obj: T, key: K): T[K] {
return obj[key];
}
/***************************
------------ 07: Union & Literal Types -----------
*******************************/
// Union Types
type StringOrNumber = string | number;
type Status = "loading" | "success" | "error";
function process(id: string | number): void {
if (typeof id === "string") {
console.log(id.toUpperCase());
} else {
console.log(id.toFixed(2));
}
}
// Intersection Types
type Person = {name: string};
type Employee = {company: string};
type Staff = Person & Employee; // has both properties
// Literal Types
type Theme = "light" | "dark";
type Port = 3000 | 8080 | 9000;
type Success = true;
// Discriminated Unions
interface Circle {
kind: "circle";
radius: number;
}
interface Square {
kind: "square";
sideLength: number;
}
type Shape = Circle | Square;
function area(shape: Shape): number {
switch (shape.kind) {
case "circle":
return Math.PI * shape.radius ** 2;
case "square":
return shape.sideLength ** 2;
}
}
/***************************
------------ 08: Type Guards & Assertions -----------
*******************************/
// Type Guards
function isString(value: any): value is string {
return typeof value === "string";
}
function isNumber(value: any): value is number {
return typeof value === "number";
}
// Using Type Guards
function process(value: string | number) {
if (isString(value)) {
console.log(value.toUpperCase()); // TypeScript knows it's string
} else {
console.log(value.toFixed(2)); // TypeScript knows it's number
}
}
// in operator
type Fish = { swim: () => void };
type Bird = { fly: () => void };
function move(animal: Fish | Bird) {
if ("swim" in animal) {
animal.swim(); // Fish
} else {
animal.fly(); // Bird
}
}
// instanceof
function handleError(error: Error | string) {
if (error instanceof Error) {
console.log(error.message);
} else {
console.log(error);
}
}
// Type Assertions
let someValue: any = "hello world";
let strLength = (someValue as string).length;
// or: let strLength = (<string>someValue).length;
// Non-null Assertion
let name: string | null = getName();
let nameLength = name!.length; // Assert name is not null
/***************************
------------ 09: Utility Types -----------
*******************************/
interface Todo {
title: string;
description: string;
completed: boolean;
}
// Partial<T> - All properties optional
type PartialTodo = Partial<Todo>;
// {title?: string, description?: string, completed?: boolean}
// Required<T> - All properties required
type RequiredTodo = Required<PartialTodo>;
// Readonly<T> - All properties readonly
type ReadonlyTodo = Readonly<Todo>;
// Pick<T, K> - Select specific properties
type TodoPreview = Pick<Todo, "title" | "completed">;
// Omit<T, K> - Exclude specific properties
type TodoInfo = Omit<Todo, "completed">;
// Record<K, T> - Create object type
type TodoStatus = Record<"pending" | "completed", Todo[]>;
// Exclude<T, U> - Remove types from union
type NonString = Exclude<string | number | boolean, string>;
// number | boolean
// Extract<T, U> - Extract types from union
type StringOnly = Extract<string | number | boolean, string>;
// string
// NonNullable<T> - Remove null/undefined
type NonNullString = NonNullable<string | null | undefined>;
// string
// ReturnType<T> - Get function return type
function getName(): string { return "John"; }
type NameType = ReturnType<typeof getName>; // string
// Parameters<T> - Get function parameters as tuple
type GetNameParams = Parameters<typeof getName>; // []
/***************************
------------ 10: Enums -----------
*******************************/
// Numeric Enum
enum Direction {
Up, // 0
Down, // 1
Left, // 2
Right // 3
}
// String Enum
enum Color {
Red = "red",
Green = "green",
Blue = "blue"
}
// Mixed Enum
enum Mixed {
No = 0,
Yes = "yes"
}
// Const Enum (inlined at compile time)
const enum StatusCode {
OK = 200,
NotFound = 404,
Error = 500
}
// Usage
let currentDirection = Direction.Up;
let favoriteColor = Color.Blue;
let status = StatusCode.OK;
/***************************
------------ 11: Modules -----------
*******************************/
// Named Exports
export const PI = 3.14159;
export function calculate(r: number): number {
return PI * r * r;
}
export class Calculator {
add(x: number, y: number): number { return x + y; }
}
// Default Export
export default class Logger {
log(message: string): void {
console.log(message);
}
}
// Re-exports
export { Calculator as Calc } from "./calculator";
export * from "./utilities";
// Import
import Logger from "./logger"; // default import
import { PI, calculate } from "./math"; // named imports
import * as MathUtils from "./math"; // namespace import
import { Calculator as Calc } from "./calculator"; // alias
// Dynamic Imports
const module = await import("./dynamic-module");
/***************************
------------ 12: Advanced Types -----------
*******************************/
// Mapped Types
type Nullable<T> = {
[P in keyof T]: T[P] | null;
};
type OptionalId<T> = {
[P in keyof T]: P extends "id" ? T[P] | undefined : T[P];
};
// Conditional Types
type IsString<T> = T extends string ? true : false;
type StringCheck = IsString<"hello">; // true
// Template Literal Types
type EventName<T extends string> = `on${Capitalize<T>}`;
type ClickEvent = EventName<"click">; // "onClick"
// Indexed Access Types
type Person = { name: string; age: number; location: string };
type PersonName = Person["name"]; // string
type PersonKeys = keyof Person; // "name" | "age" | "location"
// Recursive Types
type Json = string | number | boolean | null | Json[] | {[key: string]: Json};
/***************************
------------ 13: Decorators -----------
*******************************/
// Class Decorator
function Component(name: string) {
return function<T extends {new(...args: any[]): {}}>(constructor: T) {
return class extends constructor {
componentName = name;
};
};
}
@Component("MyComponent")
class MyClass {}
// Method Decorator
function Log(target: any, propertyName: string, descriptor: PropertyDescriptor) {
const method = descriptor.value;
descriptor.value = function(...args: any[]) {
console.log(`Calling ${propertyName} with`, args);
return method.apply(this, args);
};
}
class Service {
@Log
getData(): string {
return "data";
}
}
// Property Decorator
function MinLength(length: number) {
return function(target: any, propertyName: string) {
let value: string;
const getter = () => value;
const setter = (newVal: string) => {
if (newVal.length < length) {
throw new Error(`${propertyName} must be at least ${length} chars`);
}
value = newVal;
};
Object.defineProperty(target, propertyName, {
get: getter,
set: setter
});
};
}
class User {
@MinLength(3)
username: string;
}
/***************************
------------ 14: Configuration -----------
*******************************/
// tsconfig.json
/*
{
"compilerOptions": {
"target": "ES2020",
"module": "commonjs",
"lib": ["ES2020", "DOM"],
"outDir": "./dist",
"rootDir": "./src",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"moduleResolution": "node",
"allowSyntheticDefaultImports": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"sourceMap": true,
"declaration": true,
"removeComments": true,
"noImplicitAny": true,
"noImplicitReturns": true,
"noUnusedLocals": true,
"noUnusedParameters": true
},
"include": ["src/**/*"],
"exclude": ["node_modules", "dist"]
}
*/
// Compiler Options Quick Reference
// --target: ES5, ES6, ES2017, ES2018, ES2019, ES2020, ESNext
// --module: commonjs, amd, es2015, es2020, esnext, system, umd
// --lib: ES5, ES6, ES2017, DOM, WebWorker, ScriptHost
// --strict: Enable all strict type checking options
/***************************
------------ 15: Common Patterns -----------
*******************************/
// API Response Type
interface ApiResponse<T> {
data: T;
status: number;
message: string;
}
// Event Handler Pattern
type EventHandler<T = Event> = (event: T) => void;
const onClick: EventHandler<MouseEvent> = (e) => console.log(e.clientX);
// Builder Pattern
class QueryBuilder {
private query = "";
select(fields: string): this {
this.query += `SELECT ${fields} `;
return this;
}
from(table: string): this {
this.query += `FROM ${table} `;
return this;
}
build(): string {
return this.query.trim();
}
}
// Factory Pattern
interface Shape { area(): number; }
class Circle implements Shape {
constructor(private radius: number) {}
area(): number { return Math.PI * this.radius ** 2; }
}
class ShapeFactory {
static createCircle(radius: number): Shape {
return new Circle(radius);
}
}
// Promise/Async Patterns
type AsyncResult<T> = Promise<T | Error>;
async function fetchUser(id: number): Promise<User | null> {
try {
const response = await fetch(`/api/users/${id}`);
return await response.json();
} catch {
return null;
}
}
// Type-safe Environment Variables
interface Env {
NODE_ENV: "development" | "production" | "test";
PORT: number;
DATABASE_URL: string;
}
declare global {
namespace NodeJS {
interface ProcessEnv extends Env {}
}
}
/*
* QUICK TIPS
* ----------
* β’ Use 'unknown' instead of 'any' when possible
* β’ Prefer 'interface' for object shapes, 'type' for unions/computed types
* β’ Enable strict mode in tsconfig.json
* β’ Use const assertions: const colors = ['red', 'blue'] as const
* β’ Prefer type guards over type assertions
* β’ Use utility types instead of manual type manipulation
* β’ Enable noImplicitAny for better type safety
* β’ Use discriminated unions for complex state management
*/