Skip to content

Control Flow Analysis for Dependent Parameters doesn't work when the parameters are generic #48345

Closed
@DetachHead

Description

@DetachHead

Bug Report

πŸ”Ž Search Terms

Control Flow Analysis for Dependent Parameters generic

πŸ•— Version & Regression Information

v4.7.0-dev.20220302

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

type Func = <T extends ["a", number] | ["b", string]>(...args: T) => void;

const f1: Func = (kind, payload) => {
    if (kind === "a") {
        payload.toFixed();  // error
    }
    if (kind === "b") {
        payload.toUpperCase();  // error
    }
};

πŸ™ Actual behavior

parameter type not narrowed

πŸ™‚ Expected behavior

parameter type is narrowed, like in the example from the typescript 4.6 blog post

type Func = (...args: ["a", number] | ["b", string]) => void;

const f1: Func = (kind, payload) => {
    if (kind === "a") {
        payload.toFixed();  // 'payload' narrowed to 'number'
    }
    if (kind === "b") {
        payload.toUpperCase();  // 'payload' narrowed to 'string'
    }
};

f1("a", 42);
f1("b", "hello");

Activity

RyanCavanaugh

RyanCavanaugh commented on Mar 21, 2022

@RyanCavanaugh
Member

@ahejlsberg thoughts?

ahejlsberg

ahejlsberg commented on Mar 24, 2022

@ahejlsberg
Member

Yeah, that ought to work. Very easy fix. I'll put up a PR.

self-assigned this
on Mar 24, 2022
added this to the TypeScript 4.7.0 milestone on Mar 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

Relationships

None yet

    Participants

    @ahejlsberg@RyanCavanaugh@typescript-bot@DetachHead

    Issue actions

      Control Flow Analysis for Dependent Parameters doesn't work when the parameters are generic Β· Issue #48345 Β· microsoft/TypeScript