随笔分类 - TypeScript
摘要:One common question is whether to use type or interface in TypeScript. To clarify the difference, type can represent anything, including objects, wher
阅读全文
摘要:You cannot assign anything to never, except for never itself. // red squiggly lines under everything in parens fn("hello"); fn(42); fn(true); fn({});
阅读全文
摘要:declare const tag: unique symbol; export type EmptyObject = { [tag]?: never }; // Record<PropertyKey, never> const acceptOnlyEmptyObject = (input: Emp
阅读全文
摘要:const acceptAnythingExceptNullOrUndefined = <T>(input: {}) => {}; acceptAnythingExceptNullOrUndefined('hello'); acceptAnythingExceptNullOrUndefined(42
阅读全文
摘要:return true is a number is odd /* _____________ Your Code Here _____________ */ type LastChar<T extends string> = T extends `${infer First}${infer Res
阅读全文
摘要:ometimes you may want to determine whether a string literal is a definite type. For example, when you want to check whether the type specified as a cl
阅读全文
摘要:Implement a type IsUnion, which takes an input type T and returns whether T resolves to a union type. For example: type case1 = IsUnion<string> // fal
阅读全文
摘要:function freeze(config?: { unless: () => boolean }) { return function(target: any, propertyKey: string) { let value: any; const getter = function () {
阅读全文
摘要:Our project might have a file structure like Our project might have a file structure like data/ book.ts // A model for Book records magazine.ts // A m
阅读全文
摘要:type AnyProppertyKey = keyof any Example: type Example = Record<AnyProertyKey, any>
阅读全文
摘要:Since typescript 5, we are able to add constraints over infer. Following code doesn't apply constraints, so the inferred element could be stringand nu
阅读全文
摘要:type OneArgFn<A = any> = (firstArg: A, ..._args: any[]) => void type GetFirstArg<T> = T extends OneArgFn<infer R> ? R : never; // Test case function f
阅读全文
摘要:Particularly if you use a bundler like webpack, parcel or snowpack, you may end up importing things that aren’t .js or .ts files For example, maybe yo
阅读全文
摘要: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
阅读全文
摘要: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
阅读全文
摘要://////////////////////////////////////////////////////// // @filename: berries/raspberry.ts export class Raspberry { constructor(public color: 'red' |
阅读全文
摘要: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
阅读全文
摘要:Example code: const routingConfig = { routes: [ { path: "home", component: "HomeComponent", }, { path: "about", component: 12, }, { path: "contact", c
阅读全文
摘要:class Fish { swim(): void {} } class Bird { fly(): void {} } switch(true) { case val instanceof Bird: val.fly() break case val instanceof Fish: val.sw
阅读全文
摘要:const interValueFromColor = <N extends string, C extends string, T extends number>(colorTag: `${N}-${C}-${T}`) => { const [namespace, color, tone] = c
阅读全文