Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
DetachHead opened this issue Mar 21, 2022 · 2 comments Β· Fixed by #48411
Closed
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue

Comments

@DetachHead
Copy link
Contributor

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");
@RyanCavanaugh
Copy link
Member

@ahejlsberg thoughts?

@ahejlsberg
Copy link
Member

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

@ahejlsberg ahejlsberg self-assigned this Mar 24, 2022
@ahejlsberg ahejlsberg added the Bug A bug in TypeScript label Mar 24, 2022
@ahejlsberg ahejlsberg added this to the TypeScript 4.7.0 milestone Mar 24, 2022
@typescript-bot typescript-bot added the Fix Available A PR has been opened for this issue label Mar 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants