Skip to content

4.8.2 regression: type narrowing bug #50706

Closed
@lll000111

Description

@lll000111

Bug Report

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.
function SendBlob(data: unknown, encoding: unknown) {
    if (encoding !== undefined && encoding !== 'utf8') {
        throw new Error('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" | undefined
    encoding;
};

🔎 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.

🕗 Version & Regression Information

4.7.4 works

4.8.2 doesn't work

⏯ Playground Link

https://www.typescriptlang.org/play?ts=4.8.2#code/GYVwdgxgLglg9mABAZQKZgCYCEA2cBGAFBgIZQkBci4A1mHAO5gA0i6EcGMYA5lbfSYBKRAG8AsAChEMxDGCJC7Ttx6IAhAF5N1TKmDdUGRADITbSCt4btiAOQgowABx2RE6bK9QAFgCdGRDBUBkQAUT8Av0I7ZS5eNwBuKS8AXykU2QB6LMQAWTgQAGdURB84ADdUP0QoOEQS0oxUKFRoI1qATwAHUsyZHMQAdR90aiLVRAAVZEQKgBYAOmdFgCY5KDkisVTEAB9dZoNgjH7EQZGx4smZuaWAdkX5ja3EACJHFzf9w-1DU88MjiqmSklSoKAA

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions