随笔分类 - TypeScript
摘要:Let’s study a few examples of extends scenarios and see if we can figure out whether it will evaluate to true or false 64 extends number . . . Answer:
阅读全文
摘要:keyof The keyof type query allows us to obtain type representing all property keys on a given interface. key can be string, number or Symbol. So what
阅读全文
摘要:From source: https://www.typescript-training.com/course/intermediate-v1/03-modules/ Things can sometimes get a bit tricky when consuming CommonJS modu
阅读全文
摘要:Stacking multiple things on an identifier interface Fruit { name: string mass: number color: string } const Fruit = { name: "banana", color: "yellow",
阅读全文
摘要:`namespace` is manily for the left over from the days where we’d refer to libraries through a single global variable. With this in mind, let’s not giv
阅读全文
摘要:When working with function parameters, we know that “inner scopes” have the ability to access “outer scopes” but not vice versa function receiveFruitB
阅读全文
摘要:Assume we have the following code: interface HasId { id: string } interface Dict<T> { [k: string]: T } function listToDict<T>(list: T[]): Dict<T> { co
阅读全文
摘要:const fruits = { apple: { color: "red", mass: 100 }, grape: { color: "red", mass: 5 }, banana: { color: "yellow", mass: 183 }, lemon: { color: "yellow
阅读全文
摘要:The definite assignment !: operator is used to suppress TypeScript’s objections about a class field being used, when it can’t be proven1 that it was i
阅读全文
摘要:value is Foo The first kind of user-defined type guard we will review is an is type guard. It is perfectly suited for our example above because it’s m
阅读全文
摘要:class UnreachableError extends Error { constructor(_nvr: never, message: string) { super(message) } } class Car { drive() { console.log("vroom") } } c
阅读全文
摘要:For the following class: class Car { make: string model: string year: number constructor(make: string, model: string, year: number) { this.make = make
阅读全文
摘要:Construct signatures are similar to call signatures, except they describe what should happen with the new keyword. interface DateConstructor { new (va
阅读全文
摘要:Recursive types, are self-referential, and are often used to describe infinitely nestable types. For example, consider infinitely nestable arrays of n
阅读全文
摘要:An interface is a way of defining an object type. An “object type” can be thought of as, “an instance of a class could conceivably look like this”. Fo
阅读全文
摘要:function flipCoin(): "heads" | "tails" { if (Math.random() > 0.5) return "heads" return "tails" } function maybeGetUserInfo(): | ["error", Error] | ["
阅读全文
摘要:1. const values = [3, "14", [21]] for (let a in values) { // ^? } for (let b of values) { // ^? } . . . . Answer: for...in loop: The for...in statemen
阅读全文
摘要:TypeScript helps us catch a particular type of problem around the use of object literals. Let’s look at the situation where the error arises: // @erro
阅读全文
摘要:If<C, T, F> Implement a type that evaluates to T if the type C is true or F if C is false. // Implement this type type If<C, T, F> = C extends true ?
阅读全文
摘要:Union type: means "one of" those types Intersection type: means "combination" of those types Intersection types type typeAB = typeA & typeB; interface
阅读全文

浙公网安备 33010602011771号