Microsoft/TypeScript

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

开放

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

 (1 条评论) (0 个反应) (0 位负责人)TypeScript (13,395 个派生)batch import
BugDomain: check: Type InferenceHelp Wanted

仓库指标

星标
 (108,860 个星标)
PR 合并指标
 (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.

贡献者指南