Skip to content

Unexpected error/bad inference for generic composed function inside pipe #25791

Closed
@OliverJAsh

Description

@OliverJAsh

TypeScript Version: 2.9.2

Search Terms: index signature pipe generic lost dictionary

Code

declare function pipe<V0, T1, T2, T3>(
    fn0: (x: V0) => T1,
    fn1: (x: T1) => T2,
    fn2: (x: T2) => T3,
): (x: V0) => T3;

declare function getStringDict(): { [index: string]: string };

declare function stringDictIdentity<T>(stringDict: {
    [index: string]: T;
}): { [index: string]: T };

/*
Without strict enabled:
Expected type (x: {}) => { [index: string]: string; }
Actual type (x: {}) => { [index: string]: {}; }

With strict enabled, unexpected type error:
Argument of type '<T>(stringDict: { [index: string]: T; }) => { [index: string]: T; }' is not assignable to parameter of type '(x: {}) => { [index: string]: {}; }'.
  Types of parameters 'stringDict' and 'x' are incompatible.
    Type '{}' is not assignable to type '{ [index: string]: {}; }'.
      Index signature is missing in type '{}'.
 */
const fn = pipe(
    getStringDict,
    x => x,
    stringDictIdentity,
);

//
// Workarounds
//

declare function identity<T>(a: T): T;
const fn2 = pipe(
    getStringDict,
    identity,
    stringDictIdentity,
);

const fn3 = pipe(
    getStringDict,
    x => x,
    x => stringDictIdentity(x),
);

Metadata

Metadata

Assignees

Labels

Needs InvestigationThis issue needs a team member to investigate its status.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions