Skip to content

Inconsistency of mapped type and Parameters<T> between TS and JS #28635

@AlCalzone

Description

@AlCalzone

TypeScript Version: 3.1.4

Search Terms: mapped type parameters js

Code

// TypeScript
const Foo = Object.freeze({
  a: (arg1: string) => arg1,
  b: (arg1: string, arg2: boolean) => arg2,
});
// typeof Foo = Readonly<{
//    a: (arg1: string) => string;
//    b: (arg1: string, arg2: boolean) => boolean;
//}>

type Bar = {[K in keyof typeof Foo]: (...args: Parameters<(typeof Foo)[K]>) => void}
// typeof Bar = { 
//     readonly a: (arg1: string) => void; 
//     readonly b: (arg1: string, arg2: boolean) => void;
// }
// JavaScript
const Foo = Object.freeze({
	a: (/** @type {string} */arg1) => arg1,
	b: (/** @type {string} */arg1, /** @type {boolean} */arg2) => arg2,
});
// typeof Foo = Readonly<{
//    a: (arg1: string) => string;
//    b: (arg1: string, arg2: boolean) => boolean;
//}>
// ^ is identical to TS

/** @typedef {{[K in keyof typeof Foo]: (...args: Parameters<(typeof Foo)[K]>) => void}} Bar */
// typeof Bar = {
//     readonly a: (arg1?: string) => void;
//     readonly b: (arg1?: string, arg2?: boolean) => void;
// }
// ^ why optional??

Expected behavior:
Bar.{a,b} having the same signatures in TS and JS

Actual behavior:
Bar.{a,b} have only optional parameters in JS.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptDomain: JSDocRelates to JSDoc parsing and type generation

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions