Skip to content

[3.9.0-beta] Type gets incorrectly un-narrowed after ifs #37660

Closed
@eamodio

Description

@eamodio

TypeScript Version: 3.9.0-dev.20200327

Search Terms:

Expected behavior:
No error

Actual behavior:
Error:

Property 'name' does not exist on type 'object | Function'.
  Property 'name' does not exist on type 'object'.(2339)

Related Issues:

Code

function foo(instance: Function | object) {
	if (typeof instance === 'function') {
		if (instance.prototype == null || instance.prototype.constructor == null) {
			return instance.name;
		}
	}
}
Output
"use strict";
function foo(instance) {
    if (typeof instance === 'function') {
        if (instance.prototype == null || instance.prototype.constructor == null) {
            return instance.name;
        }
    }
}
Compiler Options
{
  "compilerOptions": {
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictPropertyInitialization": true,
    "strictBindCallApply": true,
    "noImplicitThis": true,
    "noImplicitReturns": true,
    "alwaysStrict": true,
    "esModuleInterop": true,
    "declaration": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "moduleResolution": 2,
    "target": "ES2017",
    "jsx": "React",
    "module": "ESNext"
  }
}

Playground Link: Provided

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