Microsoft/TypeScript

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

Open

#52.768 aberto em 14 de fev. de 2023

Ver no GitHub
 (1 comment) (0 reactions) (0 assignees)TypeScript (6.726 forks)batch import
BugDomain: check: Type InferenceHelp Wanted

Métricas do repositório

Stars
 (48.455 stars)
Métricas de merge de PR
 (Mesclagem média 6d 17h) (9 fundiu PRs em 30d)

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.

Guia do colaborador