Skip to content

Generic parameter is considered object #27093

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
svatal opened this issue Sep 14, 2018 · 1 comment
Closed

Generic parameter is considered object #27093

svatal opened this issue Sep 14, 2018 · 1 comment
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@svatal
Copy link

svatal commented Sep 14, 2018

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:

@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Sep 17, 2018
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 3.2 milestone Sep 17, 2018
@ahejlsberg ahejlsberg added the Fixed A PR has been merged for this issue label Sep 17, 2018
@ahejlsberg
Copy link
Member

This is fixed by #27157.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

3 participants