Skip to content

Nested try block breaks type checking #39043

Closed
@warrenfalk

Description

@warrenfalk

TypeScript Version: 4.0.0-dev.20200612

Search Terms: nested try

Code

type State
    = {tag: "one"}
    | {tag: "two"}
    | {tag: "three"}

function notallowed(arg: number) {
    let state: State = {tag: "one"};
    try {
        state = {tag: "two"};
        try { // <- this inner try causes a problem
            state = {tag: "three"}; // <- this should make "three" be a possibility but when inside a try, typescript "forgets" it.
        }
        finally {}
    }
    catch (err) {
        state.tag; // <- this should be "one" | "two" | "three" but typescript says it is "one" | "two"
        if (state.tag !== "one" && state.tag !== "two") {
            console.log(state.tag); // <- typescript reports a type error here
        }
    }
}

Expected behavior:

The example code should compile without error.

Actual behavior:

The compiler reports a type error as though the code in the inner try can't happen

Playground Link:

https://www.typescriptlang.org/play/#code/C4TwDgpgBAysCGwICgpqgXigbwQcwC4oAiAewDsJiBfVdAHx3yOOAHdSa61Hd5CSwABYAnCFVrIAZgFdyAY2ABLClHKkEAG02k2EACYAKeCIHkZAWwBGEEQEoc3KJojAoAZwRIicRNCx8AmSUNADcTsAiII7osR5e-kz8LOyc1OFx6JHR2FAA9HlQADwAtFDCSu5QSuSUIuVRUPLwMu4QVfBQYCKkVi4WTplonn6YSUHCYhKh+YWl5eDt8iJKYG5SpKauVRVVzeRQQvBgkAdsQhAHSm6V1eTuSvrQuw0gg+i0Q1BSNfDaOZ8Pk5msB5EIoIZbPYYkMRkgAHT4GYFYplF7uISkGSafRQGwkChUKCMVgcYjEwSicTkqwyNygSDuZarNzueAgKrXapVYJEkmpYjvNBKKQQuEQRH8KAAQgwWF55IAZIr4n5JXgZXLBGSHNghXF5BR3KQXPCdHhDOL1XZkXM0YsmSs1lAxGBNsAOgtIFAoZtDrYUF9AWhaJJZAplKo-jo9EYTGZLDZoXrYi5WQkfAkxoEWISwhFGinYVmAsxtWkMkM8gAqbLVwq5FHzMQWUgAN2eQluNTqryg8HIuK5HBEAGt3AAaH0dg7CLF4cFcjFYnFqDRQCzwUfQAfRfQiqQBhTQC5ifWxcXZsusKnTWaoqBsa6Yul3XvZKcMpZO1niHZdjoqjddwHj6aArBEAcwX1YM4hrH5yGjEB6xwWDYJBMEIShXV9StJF73mF5bgVClSU4UjJmpftOVZF9VxsfURTFBJ1U1eU8ygZVVQRfA2PLYgcK+WJDXuE0JXNS0WPwG0CLKdQfREHp6lUF5CRgpxQ2QIA

Related Issues:

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