Skip to content

Specialize declared generic interfaces based on parameter shape #13852

@danvk

Description

@danvk

For typing something like underscore's _.chain, it would be helpful if interfaces could be extended based on the shape of their type parameters.

For example:

// These methods are available for any T.
declare interface Chainable<T> {
  value(): T;
}

// These methods are only available for array types.
declare interface Chainable<T[]> {
  map<U>(mapFn: (t: T) => U): Chainable<U[]>;
}

Existing type definitions try to infer whether T is an array, object, array of objects, etc. But this is cumbersome and they'll never be able to do it as well as TypeScript itself.

The snippet above doesn't parse with TypeScript 2.1.5:

[ts] Operator '>' cannot be applied to types 'undefined[]' and '{ map<U>(mapFn: (t: any) => U): Chainable<U[]>; }'.

Metadata

Metadata

Assignees

No one assigned

    Labels

    In DiscussionNot yet reached consensusSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions