-
Notifications
You must be signed in to change notification settings - Fork 12.8k
"is not assignable to" even after all necessary discriminated union checks are made #32390
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
Comments
This is a design limitation caused by the fact that discriminant narrowing only considers the declared type, rather than the current type of the identifier. In your erroneous example you are relying on the composition of two discriminant narrowing's ( Ideally this would just work as expected but I think the perf regressions would be significant. |
And we don't even need the type annotation on the new variable: if (pattern.kind === PatternKind.Known) {
// And this works even if we don't declare the type
const knownPattern = pattern;
if (knownPattern.name === "Pattern1") {
// No error, pattern1_attribute is accessible
knownPattern.pattern1_attribute = "foo";
// No error, knownPattern is assignable to Pattern1
let pattern1: Pattern1 = knownPattern;
pattern1 = pattern1;
}
} Also, could you give a short description of exactly what would cause the performance regressions? Like, would it lead to a lot of second-, third-, etc. guessing during type inference? What of and/or where would it happen? |
Synthesising the type for a property on a union, such as |
Tracked by #30557 |
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
TypeScript Version: 3.5.1
Search Terms: is not assignable to, discriminated union
Code
Expected behavior: no compiler errors
Actual behavior: compiler errors
Playground Link:
http://www.typescriptlang.org/play/#code/KYDwDg9gTgLgBAYwgOwM72MgrgWzgBQEMYZgpkBpAS2QBM4BvAKDlbguQgHdk4BeOACIO3ZIIA0LNgFVkAa049+Q2QtGCmAXyZNQkWHBgBPMMALFS5AIzLmbOHJq0AXOZJlKTgHQieAbilWZEIcYFdBIndrQQD7MAsPKwB9CygqACMsUld0NOQAcwDNAN1waHhjUzdLZAAmW0CHJ1dImuo6H0VkWLZg0PDWj1qYxvioupSSNMzsuFyaQq0SvXLDEzNfZEHyZW3kGwAfaqHlsoNKs1UuvYb7RzoWhPJ22i8r0R6gkLC5mDzF4o6FbndbHHYCTY3I7vHh7EoAMywyAQMCoKDg+WAMD2AAoAJSPcaMRoAehJcAAKgB5AAiVNcVBwYAANsBQsgYI0YAALKDcODIYBcOAAUSgfKgOMEgjxRR0SDQ8DGNUJNWUmOxT2Q+JKVHhcBxyo8Xnu9D45rBng6mzxxPseoNRvIXj6ZnNAgiWqsMrt9jYZNF4ug4jgTv2kz+GSyZioqAFEHghAQCGAqFQGVZjTiWq8YeSqSjpGUgnhEAgIyz-vJYolIbDcFj8cTaao+WC6VZhgglqsldYrKVXtViWUYc+frzo694+02iYDsNOdN-AtexenVEtrsVbgACEsnBS1A5hBQnAoMBCKgUCH0NAFqGtQ3eIR2NcnwA3QhpQgdszwqgoHQOAcEIORUwbeAuGgOQQ2AD9MFJckeQgLB8m5OBCHhSxDG5MxBDDE0nBXAQ128TZBEw9IIAQkMeTMJAmSoVlj0bQhmQvQhaCMJDz2AJA2yoAAvB96xgbt0jMV9ISfHEuG5KgEAwxt5KMR8iQQQheEkzCWzbYB6HE3DgF4mSiS-H8-zgdI1PouAuEIIw8UaBVgLUWEtVcMy1QEMcdHtfUcXcrYc1dEihD2b0tz7OAAwAOW7Mha3Ump8ymQsYzjJMUxbP8YuCvZcy9CNpmjYtS3LEo-R3BK4CS4MHHfIk2L09tOyMyKYoHFLEmHaxlAKrVx2zcYbF86dGlnIA
The text was updated successfully, but these errors were encountered: