Microsoft/TypeScript

Union of functions with typed `this`, fails with no parameters

Offen

#41.213 geöffnet am 23.10.2020

 (3 Kommentare) (2 Reaktionen) (0 zugewiesene Personen)TypeScript (13.395 Forks)batch import
BugDomain: This-TypingHelp Wanted

Repository-Metriken

Stars
 (108.860 Sterne)
PR-Merge-Metriken
 (PR-Metriken ausstehend)

Beschreibung

TypeScript Version: 4.0.2

Code

interface Context {
  hello: boolean
}

type OptionsTest =
      | ((this: Context) => void)
      | ((this: Context) => PromiseLike<any>)
      | ((this: Context, done: boolean) => void)

function container (test: OptionsTest) {

}

// This does not work!

container(function () {
  console.log(this.hello);
})

// This works!

container(function (done) {
  console.log(this.hello);
})

// This works!

type Single = (this: Context) => void;
function SingleContainer (test: Single) {}
SingleContainer(function () {
  console.log(this.hello)
})

Expected behavior:

Correctly provide this type to (typed union) functions without arguments.

Actual behavior:

No this type is being provided for functions without arguments.

Playground Link

StackOverflow Link

Search Terms

  • contextually typed
  • this parameter
  • union of functions

Contributor Guide