Microsoft/TypeScript

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

Open

#52.768 aperta il 14 feb 2023

Vedi su GitHub
 (1 commento) (0 reazioni) (0 assegnatari)TypeScript (6726 fork)batch import
BugDomain: check: Type InferenceHelp Wanted

Metriche repository

Star
 (48.455 star)
Metriche merge PR
 (Merge medio 6g 17h) (9 PR mergiate in 30 g)

Descrizione

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.

Guida contributor