Skip to content

Narrowing by checking for existence of key leads to never. #38608

Closed
@MicahZoltu

Description

@MicahZoltu

TypeScript Version: 3.9.2

Search Terms:
instanceof narrow derived

Code

class CustomError extends Error { extra: string = 'hello' }
const error: Error = new CustomError()
if ('extra' in error) {
    // code path reached at runtime
    error // type: never
} else {
    // code path not reached at runtime
    error // type: Error
}

Expected behavior:
error inside the if block is still of type Error

Actual behavior:
error inside the if block is of type never

Playground Link: https://www.typescriptlang.org/play?#code/MYGwhgzhAEDCCuEAuB7AtgUQE5ZV6ApgB5IEB2AJjNrvgN6ElZgBc0yWAlmQObQC80AOQALAiBAoh0AL4AoYCjLJCOPGxp4B0MgQDucRKkxqsACgCUczgDNoZocSTNp3VbQvQ6c6L-daAegDoJABPAAcCNl0ANwIsORlCEAgCLx8-eNpoIJCIqOhNBJkgA

Related Issues:
None that seemed to match.

Metadata

Metadata

Assignees

Labels

Needs InvestigationThis issue needs a team member to investigate its status.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions