News for hackers. Stuff that matters.
Re:TypeScript 3.5 by Dylan16807 on Wednesday May 29, 2019 @04:00PM (#20044692)
This doesn't need magic powers. All you have to do is treat literal values inside type descriptions differently.
Re:TypeScript 3.5 by phiresky on Wednesday May 29, 2019 @04:09PM (#20044796)
Inside of type descriptions, literal values are always treated as literals. The problem only appears when you declare a variable _without_ an explicit type like `const foo = {k: 'x'}`, which the compiler then infers to be of type `foo: {k: string}` and not `foo: {k: 'x'}`.

Both of these are valid inferences, and it's impossible to know which one the user wants in the general case, which is why you have to explicitly say `const foo = {k: 'x' as const}`.

Re:TypeScript 3.5 by Dylan16807 on Wednesday May 29, 2019 @04:34PM (#20045111)
So the example of `false | 'x' | 'y' | undefined` turning into string is invalid?

I feel like I'm missing something.

Re:TypeScript 3.5 by phiresky on Wednesday May 29, 2019 @04:45PM (#20045247)
here's a full example. If you do this:

    function test(p: {k: false | 'x' | 'y' | undefined}) {
        // do stuff
    }
    const p = {k: 'x'} // inferred type: {k: string}
    //...
    test(p); // type error : Type 'string' is not assignable to type 'false | "x" | "y" | undefined'
    
The problem is that the type of p.k is inferred to `string` not `x`. The compiler can't know this is fine because you could call `foo(p)` in between the declaration and the `test` call, that mutates p.k.

If you add `as const` or an explicit type to p it is fine.

(I think this is what the GGP was referring to)

Re:TypeScript 3.5 by Rapzid on Wednesday May 29, 2019 @06:10PM (#20046127)
Didn't realize "as const" worked; will have to try that next time.
Re:TypeScript 3.5 by Dylan16807 on Wednesday May 29, 2019 @04:54PM (#20045347)
Thanks!