Closed
Description
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 commentedon Mar 21, 2022
@ahejlsberg thoughts?
ahejlsberg commentedon Mar 24, 2022
Yeah, that ought to work. Very easy fix. I'll put up a PR.