Microsoft/TypeScript

Function expression or method is not inferable when we have mapped type with a conditional type

Open

#60.047 geöffnet am 24. Sept. 2024

Auf GitHub ansehen
 (2 Kommentare) (0 Reaktionen) (0 zugewiesene Personen)TypeScript (6.726 Forks)batch import
Domain: check: Type InferenceHelp WantedPossible Improvement

Repository-Metriken

Stars
 (48.455 Stars)
PR-Merge-Metriken
 (Durchschn. Merge 6T 17h) (9 gemergte PRs in 30 T)

Beschreibung

🔎 Search Terms

conditional method arrow function

🕗 Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ

⏯ Playground Link

Playground Link

💻 Code

type Map<T> =  { 
  readonly [P in keyof T]: T[P] extends boolean ? "boolean":  "other"
} 
export function buildCommand<F extends Record<string, unknown>>(builderArgs: { 
  func: (p: F) => void
  params: Map<NoInfer<F>>
}) {

}
type Foo = { foo: boolean };
// Function expression does not work
buildCommand({
  func: function (p: Foo) { },
  params: {
    foo: "boolean"
  }
})

// Methods don't work
buildCommand({
  func(p: Foo) { },
  params: {
    foo: "boolean"
  }
})

// Arrow function works
buildCommand({
  func: (p: Foo) => { },
  params: {
    foo: "boolean"
  }
})

🙁 Actual behavior

For the first two calls we get an error while the third one is successful

🙂 Expected behavior

All three calls should be successful

Additional information about the issue

Removing the constraint from F (Playground Link) or changing it to F extends Record<keyof F, unknown> (Playground Link) will remove the error.

Contributor Guide