Microsoft/TypeScript

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

Open

#54,723 建立於 2023年6月21日

在 GitHub 查看
 (1 留言) (1 反應) (0 負責人)TypeScript (6,726 fork)batch import
BugDomain: This-TypingHelp Wanted

倉庫指標

Star
 (48,455 star)
PR 合併指標
 (平均合併 6天 17小時) (30 天內合併 9 個 PR)

描述

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

貢獻者指南