Microsoft/TypeScript

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

Open

#54 723 ouverte le 21 juin 2023

Voir sur GitHub
 (1 commentaire) (1 réaction) (0 assignés)TypeScript (6 726 forks)batch import
BugDomain: This-TypingHelp Wanted

Métriques du dépôt

Stars
 (48 455 stars)
Métriques de merge PR
 (Merge moyen 6j 17h) (9 PRs mergées en 30 j)

Description

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

Guide contributeur