Live data from Hacker News

TypeScript in 5 minutes

typescriptlang.org

1–10 of 33 posts

Re: TypeScript in 5 minutes

#3
I 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 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

#4
I 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 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

#5
post #3

I 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…

You’re not missing anything ; instanceof can be used to check prototype chain (class inheritance) but there is nothing similar for object interfaces (duck typing). And most Javascript objects are only loosely defined to conform to interfaces.

Re: TypeScript in 5 minutes

#6

I 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…

Typescript has flags for turning on more strictness, and I highly recommend using them. For example, strictNullChecks for your example above.

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

#7
post #3

I 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…

What you're "missing", I think, is that Typescript's types only exist at compile-time, so they aren't available through runtime operations like instanceof. It's like a more extensive case of Java generics erasure.

Re: TypeScript in 5 minutes

#8
post #3

I 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…

For types like those which are outside your control you can use type guards[1]:

    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)

Post reply on HN