top | item 39436791

(no title)

wezm | 2 years ago

It kinda does if https://www.typescriptlang.org/play#example/discriminate-typ... is what you're referring to but it appears to be missing one of the major benefits: an error/warning if you haven't handled all variants.

Edit: lots of replies showing how TypeScript can be made to do exhaustiveness checking. It's neat and all but it's a lot of gymnastics compared to languages that just have this built in, which again is part of the appeal of Gleam for me.

discuss

order

laurencerowe|2 years ago

You can workaround the lack of exhaustive matching with the following pattern:

    type Variant = { kind: "value", value: string  } | { kind: "error", error: string } | { kind: "unexpected" };

    class Unreachable extends Error {
        constructor(unexpected: never) {
            super(`${unexpected}`);
        }
    }

    function useVariant(variant: Variant) {
        switch (variant.kind) {
            case "value":
                return variant.value;
            case "error":
                return variant.error;
            default:
                throw new Unreachable(variant);
        }
    }
The `new Unreachable(variant)` will fail the type check only when you have not exhaustively matched all variants.

klavinski|2 years ago

For the most straightforward nominative pattern matching, I would write a small match function:

    type A = { kind: "kindA", a: "dataA" }
    type B = { kind: "kindB", b: "dataB" }
    type Sum = A | B

    const match = <
        const V extends { kind: string },
        const C extends { [ kind in V[ "kind" ] ]: ( value: V & { kind: kind } ) => unknown }
    >( value: V, cases: C ) => cases[ value.kind as V[ "kind" ] ]( value ) as ReturnType<C[ V[ "kind" ] ]>

    // You check the type of result, change the type of value to A or B, make the cases non-exhaustive...
    const howToUse = ( value: Sum ) => {
        const result = match( value, {
            kindA: _ => _.a,
            kindB: _ => _.b
        } )
    }
You can test it here: https://www.typescriptlang.org/play?#code/C4TwDgpgBAglC8UDeU...

dcre|2 years ago

Yeah, that can be annoying. Until pattern matching makes it into JS, the main way people deal with this is to use a library. For example, ts-pattern lets you stick `.exhaustive()` at the end of a match call and I believe you get the error at typechecking time, not runtime.

https://github.com/gvergnaud/ts-pattern

superice|2 years ago

Oh that’s easy to fake with type narrowing:

function expectType<A, B extends A>() {}

expectType<never, typeof yourUnion>();

The function call will fail at compile time if yourUnion is anything more than never, which you can use in your else case of if statements that narrow the discriminated union.