Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Type incorrectly inferred as union type when should be a string literal type #29373

Closed
gtkatakura-bysoft opened this issue Jan 11, 2019 · 3 comments
Labels
Bug A bug in TypeScript
Milestone

Comments

@gtkatakura-bysoft
Copy link

TypeScript Version: 3.2.2

Search Terms: string literal type infer

Code

type MethodNamesFrom<T> = {
  [K in keyof T]: T[K] extends Function ? K : never
}[keyof T]

const obj = {
  a: () => { },
  b: () => { },
  c: 2
}

// CASE 1: PROBLEM?
function Infer1<TObject, TKey extends MethodNamesFrom<TObject>>(o: TObject, k: TKey) {

}

Infer1(obj, 'a') // TKey infer "a" | "b"

// CASE 2: OK
function Infer2<TKey extends MethodNamesFrom<typeof obj>>(k: TKey) {

}

Infer2('a') // TKey infer "a"


// CASE 3: OK, explicity define generic types
Infer1<typeof obj, 'a'>(obj, 'a') // TKey infer "a"

// CASE 4: OK, explicity define "a" as "a"
Infer1(obj, 'a' as 'a') // TKey infer "a"

// CASE 5: OK, enforces string in TKey
function Infer3<TObject, TKey extends string & MethodNamesFrom<TObject>>(o: TObject, k: TKey) {

}

Infer3(obj, 'a') // TKey infer "a"

Expected behavior: Infer TKey as "a" | "b" in case 1

Actual behavior: Should infer TKey as "a"

Playground Link: https://www.typescriptlang.org/play/index.html#src=type%20MethodNamesFrom%3CT%3E%20%3D%20%7B%0D%0A%20%20%5BK%20in%20keyof%20T%5D%3A%20T%5BK%5D%20extends%20Function%20%3F%20K%20%3A%20never%0D%0A%7D%5Bkeyof%20T%5D%0D%0A%0D%0Aconst%20obj%20%3D%20%7B%0D%0A%20%20a%3A%20()%20%3D%3E%20%7B%20%7D%2C%0D%0A%20%20b%3A%20()%20%3D%3E%20%7B%20%7D%2C%0D%0A%20%20c%3A%202%0D%0A%7D%0D%0A%0D%0A%2F%2F%20PROBLEM%3F%0D%0Afunction%20Infer1%3CTObject%2C%20TKey%20extends%20MethodNamesFrom%3CTObject%3E%3E(o%3A%20TObject%2C%20k%3A%20TKey)%20%7B%0D%0A%0D%0A%7D%0D%0A%0D%0AInfer1(obj%2C%20'a')%20%2F%2F%20TKey%20infer%20%22a%22%20%7C%20%22b%22%0D%0A%0D%0A%2F%2F%20OK%0D%0Afunction%20Infer2%3CTKey%20extends%20MethodNamesFrom%3Ctypeof%20obj%3E%3E(k%3A%20TKey)%20%7B%0D%0A%0D%0A%7D%0D%0A%0D%0AInfer2('a')%20%2F%2F%20TKey%20infer%20%22a%22%0D%0A%0D%0A%0D%0A%2F%2F%20OK%2C%20explicity%20define%20generic%20types%0D%0AInfer1%3Ctypeof%20obj%2C%20'a'%3E(obj%2C%20'a')%20%2F%2F%20TKey%20infer%20%22a%22%0D%0A%0D%0A%2F%2F%20OK%2C%20explicity%20define%20%22a%22%20as%20%22a%22%0D%0AInfer1(obj%2C%20'a'%20as%20'a')%20%2F%2F%20TKey%20infer%20%22a%22%0D%0A%0D%0A%2F%2F%20OK%2C%20enforces%20string%20in%20TKey%0D%0Afunction%20Infer3%3CTObject%2C%20TKey%20extends%20string%20%26%20MethodNamesFrom%3CTObject%3E%3E(o%3A%20TObject%2C%20k%3A%20TKey)%20%7B%0D%0A%0D%0A%7D%0D%0A%0D%0AInfer3(obj%2C%20'a')%20%2F%2F%20TKey%20infer%20%22a%22%0D%0A

Related Issues:

@weswigham weswigham added the Bug A bug in TypeScript label Jan 11, 2019
@eweilow
Copy link

eweilow commented Jan 12, 2019

@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Mar 14, 2019
@gtkatakura
Copy link

Just to notify here that at least versions 3.3.3+ doesn't have more this bug. I think this issue can be closed.

@jakebailey
Copy link
Member

This was fixed in #29478, yes.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Bug A bug in TypeScript
Projects
None yet
Development

No branches or pull requests

6 participants