Skip to content

Narrowing lost in polymorphic function application #27259

Closed
@sledorze

Description

@sledorze

TypeScript Version: 3.1.0-dev.20180921 (next)

[email protected] <- works as expected.
[email protected] <- problem started to appear at that very version.

Search Terms:

generic inference regression

type XY = 'x' | 'y'
const x: XY = 'x'
interface Opt<T> {
  v: T
}
const foo = <T>(v: T): Opt<T> => ({ v })
const foox = foo(x) // Opt<string> instead of Opt<XY> (Regression)

const bar = <T>(v: T): Opt<typeof v> => ({ v })
const barx = bar(x) // Opt<string> instead of Opt<XY> (Regression)

const baz = <T>(v: T): T => v
const bazx = baz(x) // 'x' (OK)

Expected behavior:

Expected union type to be preserved

Actual behavior:

Union type is expanded to string

Playground Link:

This Playground Link demonstrate the CORRECT Behaviour (because previous typescript version is used in the playground)
http://www.typescriptlang.org/play/#src=type%20XY%20%3D%20'x'%20%7C%20'y'%0D%0Aconst%20x%3A%20XY%20%3D%20'x'%0D%0Ainterface%20Opt%3CT%3E%20%7B%0D%0A%20%20v%3A%20T%0D%0A%7D%0D%0Aconst%20foo%20%3D%20%3CT%3E(v%3A%20T)%3A%20Opt%3CT%3E%20%3D%3E%20(%7B%20v%20%7D)%0D%0Aconst%20foox%20%3D%20foo(x)%20%2F%2F%20Opt%3Cstring%3E%20instead%20of%20Opt%3CXY%3E%20(Regression)%0D%0A%0D%0Aconst%20bar%20%3D%20%3CT%3E(v%3A%20T)%3A%20Opt%3Ctypeof%20v%3E%20%3D%3E%20(%7B%20v%20%7D)%0D%0Aconst%20barx%20%3D%20bar(x)%20%2F%2F%20Opt%3Cstring%3E%20instead%20of%20Opt%3CXY%3E%20(Regression)%0D%0A%0D%0Aconst%20baz%20%3D%20%3CT%3E(v%3A%20T)%3A%20T%20%3D%3E%20v%0D%0Aconst%20bazx%20%3D%20baz(x)%20%2F%2F%20'x'%20(OK)%0D%0A

Related Issues:

I think this bug is very recent; I've not found anything relevant.

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issueHigh Priority

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions