Skip to content

Type predicate covariant comparison is unsound #58218

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

Closed
gabritto opened this issue Apr 16, 2024 · 1 comment
Closed

Type predicate covariant comparison is unsound #58218

gabritto opened this issue Apr 16, 2024 · 1 comment
Labels
Duplicate An existing issue was already created

Comments

@gabritto
Copy link
Member

πŸ”Ž Search Terms

type predicate comparison, type predicate variance

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried

⏯ Playground Link

https://www.typescriptlang.org/play?ts=5.5.0-dev.20240416#code/CYUwxgNghgTiAEAzArgOzAFwJYHtXwygGsQBhACgA8AueAIjDoEpaA3HLYAbgCgfRIsBBBAZ4WAM4BBAEK0qtOlDrwAPvQBGK9Q2bwAvAD54lcRPrK1murwHQ48EWMlT5NC9utXdTA8dOSHrw8LjIGZlLBdkKOoiaKljpa3oy8WIjw5KFUTL4A3jzwRSZc8AD0ZR7eWgA08BrIYmBQ+FCYyFAQEACe8Hg99QhKdDwAvjwgEBIIBcUl5ZW6dQ1NLfBtGB1dvRpDjNUjc4QkFJRMpRXwACoAFoGBGlDAY3xAA

πŸ’» Code

declare function takeC(x: "c"): void;

declare let isAB: (x: "a" | "b" | "c") => x is "a" | "b";
declare let isA: (x: "a" | "b" | "c") => x is "a";

isAB = isA;

declare let x: "a" | "b" | "c";
if (isAB(x)) {
    x; // "a" | "b", but can actually only be "a"
}
else {
    x; // "c", but can actually be "c" | "b"
    takeC(x); // This is bad
}

πŸ™ Actual behavior

Assignment isAB = isA doesn't error.

πŸ™‚ Expected behavior

Assignment isAB = isA errors because it is unsound.

Additional information about the issue

No response

@jcalz
Copy link
Contributor

jcalz commented Apr 17, 2024

duplicate of #26981

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Apr 17, 2024
@gabritto gabritto closed this as not planned Won't fix, can't repro, duplicate, stale Apr 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants