随笔分类 -  TypeScript

上一页 1 2 3 4 5 6 7 8 ··· 30 下一页
摘要:Node.js 13.2.0 introduced support for native ES modules. This means you can natively run code containing thing like import { Foo } from 'bar', use top 阅读全文
posted @ 2024-01-29 14:47 Zhentiw 阅读(25) 评论(0) 推荐(0)
摘要:Let's say we need to use a library with commonJS code. class Melon { cutIntoSlices() { } } module.exports = Melon Then we want to import this inside o 阅读全文
posted @ 2024-01-28 20:36 Zhentiw 阅读(18) 评论(0) 推荐(0)
摘要://////////////////////////////////////////////////////// // @filename: berries/raspberry.ts export class Raspberry { constructor(public color: 'red' | 阅读全文
posted @ 2024-01-28 20:14 Zhentiw 阅读(11) 评论(0) 推荐(0)
摘要:const NAME = "Matt"; TypeScript is telling us we can't redeclare the name variable because it has already been declared inside of lib.dom.d.ts. The in 阅读全文
posted @ 2024-01-23 21:32 Zhentiw 阅读(31) 评论(0) 推荐(0)
摘要:Example code: const routingConfig = { routes: [ { path: "home", component: "HomeComponent", }, { path: "about", component: 12, }, { path: "contact", c 阅读全文
posted @ 2024-01-23 21:16 Zhentiw 阅读(13) 评论(0) 推荐(0)
摘要:class Fish { swim(): void {} } class Bird { fly(): void {} } switch(true) { case val instanceof Bird: val.fly() break case val instanceof Fish: val.sw 阅读全文
posted @ 2024-01-22 14:44 Zhentiw 阅读(8) 评论(0) 推荐(0)
摘要:const interValueFromColor = <N extends string, C extends string, T extends number>(colorTag: `${N}-${C}-${T}`) => { const [namespace, color, tone] = c 阅读全文
posted @ 2024-01-22 03:08 Zhentiw 阅读(13) 评论(0) 推荐(0)
摘要:// Before declare function createFSM<TState extends string>(config: { // Without NoInfer, TS doesn't know which // TState is the source of truth initi 阅读全文
posted @ 2024-01-22 03:03 Zhentiw 阅读(32) 评论(0) 推荐(0)
摘要:// Before declare function useState<T>(status: T[]): T; const loadingStatus = useState(["loading", "idle"]) // string type // after declare function u 阅读全文
posted @ 2024-01-22 02:57 Zhentiw 阅读(14) 评论(0) 推荐(0)
摘要:const routes = { user: ["get-user", "get-all-users"], comment: ["get-comment", "get-all-comments"] } as const type Routes = typeof routes type Possibl 阅读全文
posted @ 2024-01-22 02:52 Zhentiw 阅读(15) 评论(0) 推荐(0)
摘要:Overrides A common mistake, that has historically been difficult for TypeScript to assist with is typos when overriding a class method class Car { hon 阅读全文
posted @ 2024-01-17 11:13 Zhentiw 阅读(22) 评论(0) 推荐(0)
摘要:JS private field #serialNumbercannot be accessed outside class. But within class, it is accessible. JS private field can also be used to check class i 阅读全文
posted @ 2024-01-16 16:45 Zhentiw 阅读(16) 评论(0) 推荐(0)
摘要:In recent Typescript, it is possible to define static block class Car { static nextSerialNumber = 100 static isReady = false static { // this is the s 阅读全文
posted @ 2024-01-16 16:17 Zhentiw 阅读(13) 评论(0) 推荐(0)
摘要:Sometimes we have a free-standing function that has a strong opinion around what this will end up being, at the time it is invoked. For example, if we 阅读全文
posted @ 2023-12-30 22:10 Zhentiw 阅读(22) 评论(0) 推荐(0)
摘要:interface UnionBuilder<T = never> { add: <NewValue>() => UnionBuilder<T | NewValue>, fold: () => T } declare const u: UnionBuilder; const result = u . 阅读全文
posted @ 2023-12-27 22:21 Zhentiw 阅读(19) 评论(0) 推荐(0)
摘要:type NestedNumber = number | NestedNumber[] 阅读全文
posted @ 2023-12-19 21:33 Zhentiw 阅读(16) 评论(0) 推荐(0)
摘要:const returnWhatIPassIn = <const T extends any[]>(t: T) => { return t; }; // result is any[] in TS 5.2, but ['a', 'b', 'c'] in 5.3 const result = retu 阅读全文
posted @ 2023-12-13 01:55 Zhentiw 阅读(11) 评论(0) 推荐(0)
摘要:Let's say you're creating a component that has all the props of input but needs to add a label prop. You'll need to extend from the ComponentProps typ 阅读全文
posted @ 2023-10-10 14:59 Zhentiw 阅读(14) 评论(0) 推荐(0)
摘要:PubSub is one of the most foundational patterns for reactivity. Firing an event out with publish() allows anyone to listen to that event subscribe() a 阅读全文
posted @ 2023-10-04 02:19 Zhentiw 阅读(16) 评论(0) 推荐(0)
摘要:type WidenLiteral<T> = T extends string | number | boolean ? ReturnType<T["valueOf"]> : T; type Example1 = WidenLiteral<"abc"> // string type Example2 阅读全文
posted @ 2023-09-20 01:29 Zhentiw 阅读(9) 评论(0) 推荐(0)

上一页 1 2 3 4 5 6 7 8 ··· 30 下一页