随笔分类 - TypeScript
摘要:function reverse(str: string): string; function reverse<T>(arr: T[]): T[]; function reverse<T>(stringOrArray: string | T[]): string | T[] { if (typeof
阅读全文
摘要:class Pizza { constructor(private name: string, private price: number) {} } class List<T> { private list: T[]; addItem(item: T): void { this.list.push
阅读全文
摘要:Discriminated (Tagged) Unions means that for two or more similar types, we have one common prop which can differentiate individual. interface Order {
阅读全文
摘要:interface Order { id: string; amount: number; currency: string; } interface Stripe { card: string; cvc: string; } interface PayPal { email: string; }
阅读全文
摘要:When we use class extend in typescript: // app.ts export default class App {} // bar.ts export defulat class Bar extends App { } After compiling, we w
阅读全文
摘要:tsconfig.json: { "compilerOptions": { "target": "es5", "lib": ["es6", "dom"], "module": "commonjs", "outDir": "dist", "noUnusedLocals": true, "noUnuse
阅读全文
摘要:Sometime if our tsconfig.json looks like this; { "compilerOptions": { "target": "es5", "module": "commonjs", "outDir": "dist" } } We can get all kinds
阅读全文
摘要:tsconfig-base.json: { "compilerOptions": { "target": "es5", "module": "commonjs", "lib": ["dom", "es6"], "outDir": "dist", "noImplicitAny": false } }
阅读全文
摘要:class Song { constructor(public title: string, public duration: number) { } } class Playlist { constructor(public name: string, public songs: Song[])
阅读全文
摘要:class Foo { bar() {} } const bar = new Foo() console.log(bar instanceof Foo) // true console.log(Object.getPrototypeOf(bar) Foo.prototype) // true cla
阅读全文
摘要:"Record" repersent key-value pair. type MyRecord<K extend string, T> = { [P in K]: T } Record key should be string. array[0] in javascript, even we gi
阅读全文
摘要:For example we have interface: interface Person { name: string, age?: number } 'age' is an optional prop. // will have an error since person.age is us
阅读全文
摘要:type MyPartial<T> = { [P in keyof T]?: T[P] } 'in' like a loop. Usage: function updatePerson(person: Person, prop: MyPartial<Person>) { return {...per
阅读全文
摘要:interface Person { name: string; age: number; address: {} } // gives error since we didn't define 'address' const person: Person = { name: "Wan", age:
阅读全文
摘要:interface Person { name: string; age: number; } interface ReadonlyPerson { readonly name: string; readonly age: number; } const person: ReadonlyPerson
阅读全文
摘要:const person = { name: "wan", age: 28 } type Person = typeof person type PersonKeys = keyof person // "name" | "age" type PersonKTypes = Person[Person
阅读全文
摘要:In Javascript, you know typeof: typeof [] // object typeof "" // string In Typescript it is more smart: const person = { name: "wan", age: 28 } type P
阅读全文
摘要:See the following example: const elem = document.querySelector('.click'); function handleClick(event: Event) { event.preventDefault(); console.log(thi
阅读全文
摘要:enum Sizes { Small = "small", Medium = "medium", Large = "large" } let selected: Sizes = Sizes.Samll; function updateSize(size: Sizes): void { selecte
阅读全文
摘要:Let's say we have enum defined as such: export enum WeekDayEnum { sunday = 0, monday, tuseday, wednesday, thursday, friday, saturday, } export type Da
阅读全文

浙公网安备 33010602011771号