Microsoft/TypeScript

'this' implicitly has type 'any' in a function used within a conditional expression

Open

#54.723 geöffnet am 21. Juni 2023

Auf GitHub ansehen
 (1 Kommentar) (1 Reaktion) (0 zugewiesene Personen)TypeScript (6.726 Forks)batch import
BugDomain: This-TypingHelp Wanted

Repository-Metriken

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

Beschreibung

Bug Report

🔎 Search Terms

this any implicit 2683 widen

🕗 Version & Regression Information

  • This is the behavior in every version I tried

⏯ Playground Link

Playground link with relevant code

💻 Code

// @strict: true

interface State {
  value: string;
  matches(value: string): boolean;
}

declare function macthesState(state: { value: string }, value: string): boolean;
declare function isState(state: unknown): state is State;

// this doesn't work
function test(config: unknown, prevConfig: unknown) {
  if (isState(config)) {
    return {
      ...config,
      matches: isState(prevConfig)
        ? prevConfig.matches
        : function (value: string) {
            return macthesState(this, value);
          },
    };
  }

  return config;
}

// this works
function test2(config: State) {
  return {
    ...config,
    matches: function (value: string) {
      return macthesState(this, value);
    },
  };
}

🙁 Actual behavior

'this' implicitly has type 'any' because it does not have a type annotation.(2683)

🙂 Expected behavior

inferred this type

Contributor Guide