Microsoft/TypeScript

strict mode prevents Parameters<> from inferring types for imported JS function

Open

#52,768 创建于 2023年2月14日

在 GitHub 查看
 (1 评论) (0 反应) (0 负责人)TypeScript (6,726 fork)batch import
BugDomain: check: Type InferenceHelp Wanted

仓库指标

Star
 (48,455 star)
PR 合并指标
 (平均合并 6天 17小时) (30 天内合并 9 个 PR)

描述

Bug Report

🔎 Search Terms

strict javascript Parameters

🕗 Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about strict, distributive conditional types.

💻 Code

// test2.js
module.exports = (schema, { path }) => {};
import x from "./test2.js";

function func<
  TFunc extends (...args: any[]) => any,
  TOpts extends Parameters<TFunc>[1] = Parameters<TFunc>[1]
>(f: TFunc, opts: TOpts) {}

func(x, { path: "" });
{
  "compilerOptions": {
    "allowJs": true,
    "esModuleInterop": true,
    "alwaysStrict": true,
    "strictNullChecks": true,
    "strictBindCallApply": true,
    "strictPropertyInitialization": true,
    "noImplicitAny": true,
    "noImplicitThis": true,
    "useUnknownInCatchVariables": true,
    "strict": true
  }
}

🙁 Actual behavior

When compilerOptions.strict is enabled, this code will fail with

Argument of type '{ path: string; }' is not assignable to parameter of type 'never'.

This failure does not occur if strict is disabled, even if all strict sub-properties are enabled. It seems like strict is enabling something in addition to all the properties it groups which changes how Parameters<> is evaluated for JS code (which evalutes the type to never), but I'm note sure what the problem here would be and can't find any other docs that'd support there being additional restrictions.

🙂 Expected behavior

Parameters<> should correctly infer the type for imported JS functions.

贡献者指南