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:
TypeScript Version: 3.1.0-dev.201xxxxx
Search Terms: generics, typeof, object, never
Code
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: