Skip to content

[Feature Request] Preserve comments when using Extract<keyof T, string> #31992

Open

Description

@AnyhowStep

Search Terms

mapped type, preserve comment, keyof, Extract

Suggestion

type Mapped<T> = {
  [k in keyof T]: ["some transformation", T[k]]
};

interface IFoo {
  /** The string */
  x: string;
  /** The number */
  y: number;
}
declare const mappedIFoo: Mapped<IFoo>;
//Tooltip shows "The string" as the comment
mappedIFoo.x

//////////////////////////
type Mapped2<T> = {
  [k in Extract<keyof T, string>]: ["some transformation", T[k]]
};

declare const mapped2IFoo: Mapped2<IFoo>;
//Tooltip DOES NOT show "The string" as the comment
mapped2IFoo.x

Playground

I'd like it if the fields of the mapped type could somehow preserve the comments of the fields of T, even after using Extract<keyof T, string>.

Use Cases

In my projects, there are many cases where I only want to deal with string keys and not symbol|number keys. So, I use Extract<keyof T, string> a lot. However, this does not preserve comments and makes me sad =(

Examples

//--noImplicitAny
type Mapped<T extends { [k: string]: any }> = {
  [k in keyof T]: ["some transformation", T[k]]
};
const someSymbol: unique symbol = Symbol();

interface IFoo {
  /** The string */
  x: string;
  /** The number */
  y: number;
  1: "i am a number";
  [someSymbol] : "i am a symbol"
}
declare const mappedIFoo: Mapped<IFoo>;
//Tooltip shows "The string" as the comment
mappedIFoo.x;
//Is allowed, because we use `keyof T`
mappedIFoo[1];
//Is allowed, because we use `keyof T`
mappedIFoo[someSymbol];

//////////////////////////
type Mapped2<T extends { [k : string] : any }> = {
  [k in Extract<keyof T, string>]: ["some transformation", T[k]]
};

declare const mapped2IFoo: Mapped2<IFoo>;
//Expected: Tooltip shows "The string" as the comment
//Actual:   Tooltip DOES NOT show "The string" as the comment
mapped2IFoo.x;
//Expected: is not allowed
//Actual  : Is not allowed; OK!
mapped2IFoo[1];
//Expected: is not allowed
//Actual  : Is not allowed; OK!
mapped2IFoo[someSymbol];

Playground

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

Activity

  1. AnyhowStep commented on Jun 20, 2019

    @AnyhowStep
    ContributorAuthor

    Workaround,

    //--noImplicitAny
    type Mapped3<
      T extends { [k: string]: any },
      /*
        We use a little hack to get `mapped3IFoo.x` to show its comment.
        I don't like this hack because someone could easily mess it up by modifying
        the value of `K`
      */
      K extends keyof T = Extract<keyof T, string>
    > = {
      [k in K]: ["some transformation", T[k]]
    };
    const someSymbol: unique symbol = Symbol();
    interface IFoo {
      /** The string */
      x: string;
      /** The number */
      y: number;
      1: "i am a number";
      [someSymbol] : "i am a symbol"
    }
    
    declare const mapped3IFoo: Mapped3<IFoo>;
    //Expected: Tooltip shows "The string" as the comment
    //Actual:   Tooltip shows "The string" as the comment; OK!
    mapped3IFoo.x;
    //Expected: is not allowed
    //Actual  : Is not allowed; OK!
    mapped3IFoo[1];
    //Expected: is not allowed
    //Actual  : Is not allowed; OK!
    mapped3IFoo[someSymbol];

    Playground

    I don't like this workaround because the type ends up looking like this,

    const mapped3IFoo: Mapped3<IFoo, "x" | "y">
    
  2. AnyhowStep commented on Jun 20, 2019

    @AnyhowStep
    ContributorAuthor

    Better workaround?

    //--noImplicitAny
    /**
      DO NOT USE THIS TYPE DIRECTLY!
      USE `Mapped4<>` INSTEAD!
    
      -----
    
      We can probably export `Mapped4<>` and not export `__Mapped4Impl<>`
      to force users to only use `Mapped4<>`
    */
    type __Mapped4Impl<
      T extends { [k: string]: any },
      /*
        We use a little hack to get `mapped4IFoo.x` to show its comment.
      */
      K extends keyof T = Extract<keyof T, string>
    > = {
      [k in K]: ["some transformation", T[k]]
    };
    type Mapped4<T extends { [k: string]: any }> = __Mapped4Impl<T>;
    
    const someSymbol: unique symbol = Symbol();
    interface IFoo {
      /** The string */
      x: string;
      /** The number */
      y: number;
      1: "i am a number";
      [someSymbol] : "i am a symbol"
    }
    
    declare const mapped4IFoo: Mapped4<IFoo>;
    //Expected: Tooltip shows "The string" as the comment
    //Actual:   Tooltip shows "The string" as the comment; OK!
    mapped4IFoo.x;
    //Expected: is not allowed
    //Actual  : Is not allowed; OK!
    mapped4IFoo[1];
    //Expected: is not allowed
    //Actual  : Is not allowed; OK!
    mapped4IFoo[someSymbol];

    Playground

    I don't like this workaround because the type ends up looking like this,

    const mapped4IFoo: __Mapped4Impl<IFoo, "x" | "y">
    
  3. AnyhowStep commented on Jun 20, 2019

    @AnyhowStep
    ContributorAuthor

    I also don't like the workarounds because the constraint is K extends keyof T and not K extends Extract<keyof T, string>

  4. AnyhowStep commented on Jun 20, 2019

    @AnyhowStep
    ContributorAuthor

    This is my favourite workaround so far,

    //--noImplicitAny
    /**
      We use this no-op `_` type as a hack to get the tooltip to give
      us a "better-looking" type
    */
    type _<T> = T;
    /**
      DO NOT USE THIS TYPE DIRECTLY!
      USE `Mapped5<>` INSTEAD!
    
      -----
    
      We can probably export `Mapped5<>` and not export `__Mapped5Impl<>`
      to force users to only use `Mapped5<>`
    */
    type __Mapped5Impl<
      T extends { [k: string]: any },
      /*
        We use a little hack to get `mapped5IFoo.x` to show its comment.
      */
      K extends keyof T=Extract<keyof T, string>
    > = _<{
      [k in K]: ["some transformation", T[k]]
    }>;
    type Mapped5<T extends { [k: string]: any }> = __Mapped5Impl<T>;
    
    const someSymbol: unique symbol = Symbol();
    interface IFoo {
      /** The string */
      x: string;
      /** The number */
      y: number;
      1: "i am a number";
      [someSymbol] : "i am a symbol"
    }
    
    /**
      Tooltip shows,
      const mapped5IFoo: { x: ["some transformation", string]; y: ["some transformation", number]; }
    */
    declare const mapped5IFoo: Mapped5<IFoo>;
    //Expected: Tooltip shows "The string" as the comment
    //Actual:   Tooltip shows "The string" as the comment; OK!
    mapped5IFoo.x;
    //Expected: is not allowed
    //Actual  : Is not allowed; OK!
    mapped5IFoo[1];
    //Expected: is not allowed
    //Actual  : Is not allowed; OK!
    mapped5IFoo[someSymbol];

    Playground

    The type looks like this,

    const mapped5IFoo: { x: ["some transformation", string]; y: ["some transformation", number]; }

    However, to get it to "look nice" and "work", I needed 3 types instead of 1, and weird indirection.


    And I still don't like that the constraint is K extends keyof T (even though leaving __Mapped5Impl<> unexported mitigates the risk of K being changed to something else).

  5. AnyhowStep commented on Jun 20, 2019

    @AnyhowStep
    ContributorAuthor

    I'm kinda' obsessed with the constraint being Extract<keyof T, string> because of something like this,

    type _<T> = T;
    type TakesNumber<N extends number> = (
      N extends number ?
      ["some transformation", N] :
      ["Expected number, received", N]
    )
    type Mapped6<
      T extends { [k: string]: number }
    > = _<{
      //TS thinks `T[k] extends number` but it may not be the case!
      [k in keyof T]: TakesNumber<T[k]>
    }>;
    
    const someSymbol: unique symbol = Symbol();
    
    /**
      const mapped6IFoo: {
        x: ["some transformation", number];
        y: ["some transformation", number];
        //Whoops! Using `keyof T` was not such a good idea!
        [someSymbol]: ["Expected number, received", "string value of symbol key"];
      }
    */
    declare const mapped6IFoo: Mapped6<{
      x: number,
      y: number,
      [someSymbol] : "string value of symbol key",
    }>;

    Playground

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

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions