Skip to content

Generic parameter is considered object #27093

Closed
@svatal

Description

@svatal

TypeScript Version: 3.1.0-dev.201xxxxx

Search Terms: generics, typeof, object, never

Code

function f<TData>(data: TData): boolean {
  if (typeof data === "object") return true;
  return data;
}

let x: boolean = f(1);

Expected behavior:
Code should not compile - function actually returns boolean | TData, but states only boolean

Actual behavior:
Code compiles without errors. After the if statement, data is considered never

Playground Link: http://www.typescriptlang.org/play/#src=function%20f%3CTData%3E(data%3A%20TData)%3A%20boolean%20%7B%0D%0A%20%20%20%20if%20(typeof%20data%20%3D%3D%3D%20%27object%27)%0D%0A%20%20%20%20%20%20%20%20return%20true%3B%0D%0A%20%20%20%20return%20data%3B%0D%0A%7D%0D%0A%0D%0Alet%20x%3A%20boolean%20%3D%20f(1)%3B

Related Issues:

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions