Skip to content

Generic literal marker not obeyed for unknown and {} #32169

Closed
@evmar

Description

@evmar

TypeScript Version: 3.4 up to 3.6-dev20190628

Search Terms:
generic literal unknown

Code

This is expected:

function f<T>(x: T): T { return x; }
enum E { A, B }
let x = f(E.A);  // x has type E, not E.A

According to #24919 (comment) , you can get this to produce an E.A if you add an extends marker:

function f<T extends {}|null|undefined>(x: T): T { return x; }
enum E { A, B }
let x = f(E.A);  // x now has type E.A

Expected behavior:

I'd expect the same literal inference if you write these forms too:

function f<T extends unknown>(x: T): T { return x; }
function f<T extends {}>(x: T): T { return x; }

Actual behavior:

It has the other behavior, as if you have no extends clause

Playground Link:

http://www.typescriptlang.org/play/#code/FAUwdgrgtgBAojA3jAggGhgIRgX2MAMwjAGMAXASwHswYCBGAHgBUA+ACgA8AuGZgSl7MkMAE4gyEUbU4BuXMAA2EmJ3owAvHXrs4AOhT95MAPQnVMABYBDAM4wyATwAOIePiKlKNOgCYWMCCcZOAAJvaIOAA+kIqKUcShIAQUYCChHDx8gnwi4pLSqvJ4ymSqvpp+ugZGMKbmnFZ2Di5u+igexOTUtAQAzAFBIWDhMMQA1mBUAO5gmUI5wsj5UjLFSiqcfZX91YbGZhY29k6u8DAA-BcAhEA

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