microsoft / microsoft/TypeScript

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

Open
#31,992 5 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Experience Enhancement Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the supplied TypeScript examples and Playgrounds, comparing the tooltip behavior of Mapped with Mapped2 using Extract<keyof T, string>. Done means comments such as “The string” and “The number” are preserved for mapped fields while number and symbol keys remain excluded; no repository files or tests are named.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.