Skip to content

Invalid declarations for inlined distributive types in 4.7Β #49591

@dragomirtitian

Description

@dragomirtitian

Bug Report

πŸ”Ž Search Terms

invalid declaration distributive types

πŸ•— Version & Regression Information

  • This changed between versions 4.6 and 4.7

⏯ Playground Link

Workbench

Playground Link For resulting declarations

πŸ’» Code

// @filename: types.ts
type Fns =  Record<string, (...params: unknown[]) => unknown>

type Map<T extends Fns> = { [K in keyof T]: T[K]; };

type AllArg<T extends Fns> = { [K in keyof T]: Parameters<T[K]> };

function fn<T extends { x: Map<T['x']> }>(sliceIndex: T): AllArg<T['x']> {
    return null!;
}

export default { fn };

// @filename: reexport.ts

import test from "./types";
export default { test };

πŸ™ Actual behavior

TypeScript generates this declaration:

declare const _default: {
    test: {
        fn: <T_1 extends {
            x: T_1["x"] extends infer T ? { [K in keyof T]: T_1["x"][K]; } : never;
        }>(sliceIndex: T_1) => T_1["x"] extends infer T_2 ? { [K_1 in keyof T_2]: Parameters<T_1["x"][K_1]>; } : never;
    };
};
export default _default;

In the declaration above the expression Parameters<T_1["x"][K_1]> doesn't type check. The T_1["x"] is not constrained anymore to be Record<string, (...params: unknown[]) => unknown>

TypeScript could include the constraint of the original type in the declaration:

declare const _default: {
    test: {
        fn: <T_1 extends {
            x: T_1["x"] extends (infer T extends Record<string, (...params: unknown[]) => unknown>) ? { [K in keyof T]: T_1["x"][K]; } : never;
        }>(sliceIndex: T_1) => T_1["x"] extends (infer T_2 extends Record<string, (...params: unknown[]) => unknown>)
             ? { [K_1 in keyof T_2]: Parameters<T_1["x"][K_1]>; } : never;
    };
};
export default _default;

πŸ™‚ Expected behavior

Resulting declarations for reexport.ts are valid

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions