You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Instead of words, just the code. The comments say what is wrong and what used to work in 4.7.4.
// Arguments are set to "unknown" because they receive data from network requests// so runtime checks are mandatory to narrow type before use.functionSendBlob(data: unknown,encoding: unknown){if(encoding!==undefined&&encoding!=='utf8'){thrownewError('encoding');}// Mouse hover to see detected type // When using TS v4.8.2 it is {} | undefined// When using TS v4.7.4 it is "utf8" | undefinedencoding;};
🔎 Search Terms
"4.8.2" and I found a few but was told to file a new issue because I'm told it wasn't similar to the ones that I found and fought were similar.
I thought e.g. #50671 looks similar, not for the code but possibly for the deeper cause, since I don't think that many things changed in exactly this part of TS since 4.7.4.
Hah, this is a fun one. Switch the order of your checks:
if(encoding!=='utf8'&&encoding!==undefined){
Narrowing unknown has several special rules. A new one in 4.8 is that unknown can break down into a union of {} | null | undefined. That’s triggered as soon as you filter undefined out of it in your first check, and at that point, you’re left with {} | null, and so the next narrowing step can only discriminate between those two things. But if you reverse the order, a different set of special rules around strict equality in general takes effect first.
functiondoSomething<Textendsunknown>(value: T): T{if(value===undefined){returnvalue;}// ERROR: This condition will always return false because...if(value===42){throwError('Meaning of life value');}returnvalue;}
Or, without generics, without error but the type is wrong regardless, simply
Actually, the issue reported here is a separate and unrelated issue. In 4.8, the check for undefined narrows the type of T to T & ({} | null), but that should still be comparable to 42.
Anders Hejlsberg (@ahejlsberg) This has not been fixed in the new 4.8.4. If I follow the "closed as completed" link to a merge it shows 4.8.4 as the target release for that merge though.
There is 4.9.0 as "Milestone" on this issue, but the link to PR #50735 as the latest log entry just above has 4.8.4?
Activity
andrewbranch commentedon Sep 9, 2022
Hah, this is a fun one. Switch the order of your checks:
Narrowing
unknownhas several special rules. A new one in 4.8 is thatunknowncan break down into a union of{} | null | undefined. That’s triggered as soon as you filterundefinedout of it in your first check, and at that point, you’re left with{} | null, and so the next narrowing step can only discriminate between those two things. But if you reverse the order, a different set of special rules around strict equality in general takes effect first.lll000111 commentedon Sep 9, 2022
Ah I have another one, single check for
undefinedcondition:Playground
Or, without generics, without error but the type is wrong regardless, simply
Playground
andrewbranch commentedon Sep 9, 2022
Yeah, that’s the exact same issue.
Not wrong, just not as specific as it could be.
42is assignable to{}.{}just means not null or undefined.42is not null or undefined.ahejlsberg commentedon Sep 12, 2022
Actually, the issue reported here is a separate and unrelated issue. In 4.8, the check for
undefinednarrows the type ofTtoT & ({} | null), but that should still be comparable to42.lll000111 commentedon Oct 6, 2022
Anders Hejlsberg (@ahejlsberg) This has not been fixed in the new 4.8.4. If I follow the "closed as completed" link to a merge it shows 4.8.4 as the target release for that merge though.
There is 4.9.0 as "Milestone" on this issue, but the link to PR #50735 as the latest log entry just above has 4.8.4?
ahejlsberg commentedon Oct 6, 2022
I think the intent was to fix it in 4.8.4, but you're right, it isn't in that release. It's definitely fixed in 4.9.
Andrew Branch (@andrewbranch) Looks like there was a failed cherry pick into 4.8.4, but not sure if that was the reason.