Skip to content

Filter keyof properties by types #13214

Closed

Description

So there is:

type Deferred<T> = {
    [P in keyof T]: Promise<T[P]>;
};

And we have:

interface Obj
{
    id: number;

    employee: {
        id: number;
        name: string;
    }

    // an array here too
}

With Deferred<Obj> the employee object and arrays will also be Promise but that would not always be what we want. It also will not nest on children objects.

The ability to filter keys by type would be very good as:

type Container<T> = {
    [P in keyof T where T[P] extends number | string]: Promise<T[P]>;
    [P in keyof T where T[P] extends {}]: Container<T[P]>;
}

If use more than one in would be out of question, something using intersection may work too.

Activity

  1. ahejlsberg commented on Dec 29, 2016

    @ahejlsberg
    Member

    Yes, this would be a nice capability to have. I think it is already covered by #12424.

  2. asfernandes commented on Dec 30, 2016

    @asfernandes
    Author

    I understand the similarity and depending on the design of #12424 it will cover my needs. On the other hand, this issue may do additional things, i.e., really "filter" out (hide) what comes from in/keyof depending on a condition.

  3. locked and limited conversation to collaborators on Jun 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions