Microsoft/TypeScript

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

Open

#52,768 opened on Feb 14, 2023

View on GitHub
 (1 comment) (0 reactions) (0 assignees)TypeScript (48,455 stars) (6,726 forks)batch import
BugDomain: check: Type InferenceHelp Wanted

Description

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.

Contributor guide