Skip to content

type guard does not analyze correctly when in an unreachable code block and allowUnreachableCode is true #57423

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
thomasrea0113 opened this issue Feb 16, 2024 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@thomasrea0113
Copy link

πŸ”Ž Search Terms

type guard unreachable code allowUnreachableCode

πŸ•— Version & Regression Information

Version 5.3.3

⏯ Playground Link

https://www.typescriptlang.org/play?allowUnreachableCode=true&ts=5.4.0-dev.20240215#code/PTAEHUFMBsGMHsC2lQBd5oBYoCoE8AHSAZVgCcBLA1UABWgEM8BzM+AVwDsATAGiwoBnUENANQAd0gAjQRVSQAUCEmYKsTKGYUAbpGF4OY0BoadYKdJMoL+gzAzIoz3UNEiPOofEVKVqAHSKymAAmkYI7NCuqGqcANag8ABmIjQUXrFOKBJMggBcIYqohCgAgqAAvKAA3oqgDWL5oIKolJzMigC+wSVEoABCVaAVAGS19Y3Sza3tzADc3cEInK0igkPVABQMzWUAlM3iopsAfGilKaA7YsJmePsB0lWV1QBEsxnMb8vwqzQSeSYACqnCcDFM0ncwx2e32VVOijqjREqS2bXYkHhTlQ7DInEWkwaK0E8HcAWg8GYWze2GglMk8DI0Te+2CKIoaKEAx2+zZKJRJLJkApVJ2TzZPUUJIBQI4qFB4Mh0O2uxG8MqiORjSF5Mp1NpMAZEiZLLZRNR125vP5AuJf1JerFDAlSwaQA

πŸ’» Code

// Welcome to the TypeScript Playground, this is a website
// which gives you a chance to write, share and learn TypeScript.

// You could think of it in three ways:
//
type A = {
    a: string
}

type B = A & {
    b: string;
}

const isB = (a: A): a is B => typeof (a as any).b === "string"

const withUnreachable = (a: A) =>
{
    if (true) return;

    console.log("hello world")

    if (isB(a))
        console.log(a.b)
}

const withoutUnreachable = (a: A) =>
{
    console.log("hello world")

    if (isB(a))
        console.log(a.b)
}
   

πŸ™ Actual behavior

type error is thrown because the type guard is not executed

πŸ™‚ Expected behavior

type guards should be processed even if the code is unreachable, or all type checking should be disable for unreachable code

Additional information about the issue

I tested with all versions available on the playground, none work. Full type checking is not executed on unreachable code even when allowUnreachableCode is true. It seems that the type guards do not infer the guarded type if it is in an unreachable block, so I get a compile error. This isn't a huge deal but during testing sometimes it's easier to force an early return.

@fatcerberus
Copy link

Duplicate #26914

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Feb 16, 2024
@typescript-bot
Copy link
Collaborator

This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Feb 19, 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

4 participants