TypeScript in 5 minutes
typescriptlang.org
TypeScript in 5 minutes
1–10 of 33 posts
Re: TypeScript in 5 minutes
#2Re: TypeScript in 5 minutes
#3For example, in React there is this type that's a union of an array, single object or null. Some kind of catch-all, I guess. So, when you see one of these things, there's no way to know what you have unless you do something ugly like this:
if ((value as any).length) {
// it's an array
} else if (value) {
// must be a single object, I guess..
} else {
// null
}
The thought of littering my code with these kinds of type checks makes me feel really dirty, unless I'm totally missing something...Re: TypeScript in 5 minutes
#4It is also the best place to start evaluating how "complete" a solution is; particularly when faced with multiple choices.
It was a tutorial very similar to this one that led me to choose flow over typescript - I did the equivalent of setting the "user" in this example to null, flow would tell me it wouldn't work and typescript just happily compiled away. In short, this kind of quick evaluation led me to deduce typescript is JS with loose types sometimes (but you aren't sure where) so as not to be annoying and flow is JS with restrictive types everywhere (that may not get your indirect but correct logic) so as to provide the most guarantees.
Re: TypeScript in 5 minutes
#5I just started a side project with Typescript, and I like a lot of it, but, ironically, type discovery seems _very_ lacking. The JS keywords like `instanceof` are useless because everything is just an object, and Typescript doesn't give you a way to pattern match on types. For example, in React there is this type that's a union of an array, single object or null. Some kind of catch-all, I guess. So, when you see one…
Re: TypeScript in 5 minutes
#6I really like this kind of introduction; they let you get into what the authors consider the main reason for their products existence straight away - this is what they will spend most time on and other features will exist at these features expense. It is also the best place to start evaluating how "complete" a solution is; particularly when faced with multiple choices. It was a tutorial very similar to this one that…
There’s a “strict” flag that you can use to turn on most of the more specific strictness flags all at once.
Re: TypeScript in 5 minutes
#7I just started a side project with Typescript, and I like a lot of it, but, ironically, type discovery seems _very_ lacking. The JS keywords like `instanceof` are useless because everything is just an object, and Typescript doesn't give you a way to pattern match on types. For example, in React there is this type that's a union of an array, single object or null. Some kind of catch-all, I guess. So, when you see one…
Re: TypeScript in 5 minutes
#8I just started a side project with Typescript, and I like a lot of it, but, ironically, type discovery seems _very_ lacking. The JS keywords like `instanceof` are useless because everything is just an object, and Typescript doesn't give you a way to pattern match on types. For example, in React there is this type that's a union of an array, single object or null. Some kind of catch-all, I guess. So, when you see one…
if (value == null) {
// TS knows value is null, doesn't let you access value .length for example
} else if (Array.isArray(value)) {
// TS knows value is an array
} else {
// TS knows that it's an object here
}
However, having types like those in your own code (e.g. things like X[] | Y | null) is probably an antipattern and should be avoided. Using discriminated unions[2] is much better.[1]: https://www.typescriptlang.org/docs/handbook/advanced-types.... ("Type Guards and Differentiating Types" section) [2]: [1]: https://www.typescriptlang.org/docs/handbook/advanced-types.... ("Discriminated Unions" section)
Re: TypeScript in 5 minutes
#9Article should mention that TypeScript is made by Microsoft.
Re: TypeScript in 5 minutes
#10Article should mention that TypeScript is made by Microsoft.