TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

Design Meeting Notes, 10/18/2024

Open DanielRosenwasser opened this issue 1 year ago • 0 comments

Conditional Type Narrowing

#56941

  • Shouldn't be any perf impact for existing code because most code doesn't actually use a conditional type as the return type of a function, or it just casts.

  • It is a semantic check on the return type (not syntax - you can use type aliases).

  • If we want to support this kind of narrowing, we need to dive into the conditional expression and check each branch separately.

    • Original PR did this for every function, caught bugs - but maybe we generalize there later on. For now, the current check is only when checking against a a conditional return type.
  • How does this work with contextual types?

    function makeEquals<T extends string | number>(x: T):
        T extends string
            ? (x: string) => boolean
            : (x: number) => boolean {
    
        // are these well-typed?
        return typeof x === "string" ? (y => x === y) : (y => x === y);
    }
    
    • Currently it does work. See PR for details.
  • Seems like no objections to current design - plan for 5.8.

DanielRosenwasser avatar Oct 18 '24 23:10 DanielRosenwasser