随笔分类 - TypeScript
摘要:For example: interface Todo { title: string description: string completed: boolean } type TodoPreview = MyPick<Todo, 'title' | 'completed'> const todo
阅读全文
摘要:Step 7: Tests for Types The assertion type code need to be tested just as the normal code. export function isITeam(arg: any): arg is ITeam { /** * { i
阅读全文
摘要:Step1 & 2 for converting a js app to ts Step 3. Turn on "noImplicitAny" and even more strict mode Step 4. ESLint for Typescript Step5. Local types ove
阅读全文
摘要:Step 5: Types at Runtime This problem often happens when APi return the data. let cachedAllTeamsList: Promise<ITeam[]>; export async function getAllTe
阅读全文
摘要:Step5. Local types override You can find many @types package along with the library you use. But the problem is that those @types might contain bugs b
阅读全文
摘要:Step 4: ESLint We need to install ESLint tools for Typescript. yarn add -D @typescript-eslint/eslint-plugin @typescript-eslint/parser .eslintrc file:
阅读全文
摘要:Step 3: Turn on "noImplicitAny" From previous steps, we allow implicit any: https://www.cnblogs.com/Answer1215/p/16634618.html Now, we need to turn on
阅读全文
摘要:1. Compiling in "loose mode" Start with all tests passing Rename all .js to .ts, allowing implicit any Fix only things that are not type-checking, or
阅读全文
摘要:import type only imports declarations to be used for type annotations and declarations. It always gets fully erased, so there’s no remnant of it at ru
阅读全文
摘要:function isError(err: any): err is Error { return err instanceof Error; } try { somethingRisky() } catch(err: unknown) { if (isError(err)) { console.l
阅读全文
摘要:In some ways // @ts-expect-error can act as a suppression comment, similar to // @ts-ignore. The difference is that // @ts-ignore will do nothing if t
阅读全文
摘要:type Corner = `${'top' | 'bottom'} - ${'left' | 'right'}` type Corner = Capitalize<`${'top' | 'bottom'} - ${'left' | 'right'}`> // "Top - left" | "Top
阅读全文
摘要:V3: type JSONValue = | string | number | boolean | null | JSONArray | JSONObject; type JSONArray = JSONValue[]; type JSONObect = { [k: string]: JSONVa
阅读全文
摘要:Let's see the unlabelled tuple type: type Address = [ number, string, string, number, ] function printAddress(...address: Address) { console.log(addre
阅读全文
摘要:List all the props with begin with "query" key in Document type queryTypes = Extract<keyof Document, `query${string}`> type queryyPoprDoc = { [Key in
阅读全文
摘要:type PartOfWindow = { [Key in | "document" | "navigator" | "setTimeout"]: Window[Key] } /* type PartOfWindow = { document: Document; navigator: Naviga
阅读全文
摘要:Index Signature type Fruit = { name: string color: string mass: number } type Dict<T> = { [k: string]: T } // <- index signature const fruitCatalog: D
阅读全文
摘要:Indexed Access types provide a mechanism for retrieving part(s) of an array or object type via indices. We’ll look at how this kind of type works, and
阅读全文
摘要:For example we have a Webpack class: class WebpackCompiler { constructor(options: { amd?: false | { [index: string]: any } bail?: boolean cache?: | bo
阅读全文
摘要:Extract is useful for obtaining some sub-part of a type that is assignable to some other type. type FavoriteColors = | "dark sienna" | "van dyke brown
阅读全文