Skip to content

Allow negated types + F-bounded mapped types #18280

Description

Edit: keyof works as I thought; mapped types are just broken in interfaces. I also clarified a few things.

Allowing negated types and F-bounded mapped type variables would solve several issues:

Here's my proposal:

  1. Allow negated types, using !T to represent the set of all types that do not conform to T (i.e. not assignable to T). This is pretty simple to digest.

  2. Expand mapped types to allow the key variable to be F-bounded, like in {[P in Keys<T, P>]: ...}.

The first is sufficient to cover subtraction types and Promise type issues, and the other two make mapped conditional types possible.

Activity

  1. aluanhaddad commented on Sep 6, 2017

    @aluanhaddad
    Contributor

    Big 👍 on the proposed syntax. I think the negation operator is indeed quite intuitive in the context of mapped types/keyof and is a great idea. My only concern is that I'm not sure what it means outside of an intersection context. "Does not conform to T" could be construed as meaning not assignable to T which is very different from meaning not having any of the keys of T. This may just need further clarification.

    Minor nitpick, but I believe your second item is already valid today.

  2. dead-claudia commented on Sep 7, 2017

    @dead-claudia
    Author

    Aluan Haddad (@aluanhaddad)

    My only concern is that I'm not sure what it means outside of an intersection context.

    This comment should explain type negation outside a union context (partial Promise fix).

    "Does not conform to T" could be construed as meaning not assignable to T which is very different from meaning not having any of the keys of T.

    "not assignable to T" is actually the intended meaning for negated types. This happens to be equivalent to "not having any of the keys of T" in the context of object types, but it carries more expected semantics for primitives.

    Minor nitpick, but I believe your second item is already valid today.

    Yeah...I figured this out; I just ran into this odd bug.

  3. changed the title [-]Allow negated types + expand `keyof` to accept any type[/-] [+]Allow negated types + F-bounded mapped types[/+] on Sep 7, 2017
  4. SalathielGenese commented on Mar 9, 2018

    @SalathielGenese

    I upvote A & !B especially the !B part... Over Exclude from #21847

  5. mhegazy commented on Mar 9, 2018

    @mhegazy
    Contributor

    Looks like the same feature as #4196, just different proposal. i suggests adding the proposal to #4196 and keeping the discussion to one issue.

  6. typescript-bot commented on Mar 23, 2018

    @typescript-bot
    Contributor

    Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

  7. locked and limited conversation to collaborators on Jul 25, 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