随笔分类 -  TypeScript

上一页 1 ··· 16 17 18 19 20 21 22 23 24 ··· 30 下一页
摘要:For example: interface Todo { title: string description: string completed: boolean } type TodoPreview = MyPick<Todo, 'title' | 'completed'> const todo 阅读全文
posted @ 2022-08-31 15:23 Zhentiw 阅读(42) 评论(0) 推荐(0)
摘要: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 阅读全文
posted @ 2022-08-30 18:09 Zhentiw 阅读(52) 评论(0) 推荐(0)
摘要: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 阅读全文
posted @ 2022-08-30 18:09 Zhentiw 阅读(25) 评论(0) 推荐(0)
摘要:Step 5: Types at Runtime This problem often happens when APi return the data. let cachedAllTeamsList: Promise<ITeam[]>; export async function getAllTe 阅读全文
posted @ 2022-08-30 15:32 Zhentiw 阅读(25) 评论(0) 推荐(0)
摘要: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 阅读全文
posted @ 2022-08-30 15:02 Zhentiw 阅读(23) 评论(0) 推荐(0)
摘要:Step 4: ESLint We need to install ESLint tools for Typescript. yarn add -D @typescript-eslint/eslint-plugin @typescript-eslint/parser .eslintrc file: 阅读全文
posted @ 2022-08-29 19:27 Zhentiw 阅读(370) 评论(0) 推荐(0)
摘要: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 阅读全文
posted @ 2022-08-29 18:49 Zhentiw 阅读(25) 评论(0) 推荐(0)
摘要: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 阅读全文
posted @ 2022-08-29 01:23 Zhentiw 阅读(68) 评论(0) 推荐(0)
摘要: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 阅读全文
posted @ 2022-08-24 14:44 Zhentiw 阅读(100) 评论(0) 推荐(0)
摘要:function isError(err: any): err is Error { return err instanceof Error; } try { somethingRisky() } catch(err: unknown) { if (isError(err)) { console.l 阅读全文
posted @ 2022-08-24 14:23 Zhentiw 阅读(26) 评论(0) 推荐(0)
摘要: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 阅读全文
posted @ 2022-08-24 01:46 Zhentiw 阅读(548) 评论(0) 推荐(0)
摘要:type Corner = `${'top' | 'bottom'} - ${'left' | 'right'}` type Corner = Capitalize<`${'top' | 'bottom'} - ${'left' | 'right'}`> // "Top - left" | "Top 阅读全文
posted @ 2022-08-22 16:01 Zhentiw 阅读(31) 评论(0) 推荐(0)
摘要:V3: type JSONValue = | string | number | boolean | null | JSONArray | JSONObject; type JSONArray = JSONValue[]; type JSONObect = { [k: string]: JSONVa 阅读全文
posted @ 2022-08-22 15:55 Zhentiw 阅读(27) 评论(0) 推荐(0)
摘要:Let's see the unlabelled tuple type: type Address = [ number, string, string, number, ] function printAddress(...address: Address) { console.log(addre 阅读全文
posted @ 2022-08-22 15:03 Zhentiw 阅读(21) 评论(0) 推荐(0)
摘要:List all the props with begin with "query" key in Document type queryTypes = Extract<keyof Document, `query${string}`> type queryyPoprDoc = { [Key in 阅读全文
posted @ 2022-08-21 00:21 Zhentiw 阅读(34) 评论(0) 推荐(0)
摘要:type PartOfWindow = { [Key in | "document" | "navigator" | "setTimeout"]: Window[Key] } /* type PartOfWindow = { document: Document; navigator: Naviga 阅读全文
posted @ 2022-08-18 19:16 Zhentiw 阅读(27) 评论(0) 推荐(0)
摘要:Index Signature type Fruit = { name: string color: string mass: number } type Dict<T> = { [k: string]: T } // <- index signature const fruitCatalog: D 阅读全文
posted @ 2022-08-17 15:08 Zhentiw 阅读(39) 评论(0) 推荐(0)
摘要: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 阅读全文
posted @ 2022-08-16 21:18 Zhentiw 阅读(38) 评论(0) 推荐(0)
摘要:For example we have a Webpack class: class WebpackCompiler { constructor(options: { amd?: false | { [index: string]: any } bail?: boolean cache?: | bo 阅读全文
posted @ 2022-08-16 21:07 Zhentiw 阅读(19) 评论(0) 推荐(0)
摘要: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 阅读全文
posted @ 2022-08-15 18:53 Zhentiw 阅读(84) 评论(0) 推荐(0)

上一页 1 ··· 16 17 18 19 20 21 22 23 24 ··· 30 下一页