Skip to content

Design Meeting Notes, 10/18/2024 #60287

Open
@DanielRosenwasser

Description

@DanielRosenwasser

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design NotesNotes from our design meetings

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions