Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow any key type as an index signature parameter type #26797

Open
wants to merge 17 commits into
base: master
from

Conversation

@weswigham
Copy link
Member

commented Aug 30, 2018

This allows, eg, symbol:

interface SymbolMap {
  [x: symbol]: string;
}

interface PropertyMap {
  [x: string | number | symbol]: string;
}

// same as
interface PropertyMap {
  [x: PropertyKey]: string;
}

enums:

enum Cardinal {
  North = "n",
  South = "s",
  West = "w",
  East = "e"
}
interface CardinalDataMap {
  [dir: Cardinal]: number;
}

enum Places {
  None,
  First,
  Second,
  Third
}
interface PlaceMap {
  [dir: Places]: number;
}

literal unions:

interface AdjacencyMap {
  [side: "top" | "bottom" | "left" | "right"]: number;
}

and generics:

interface PartialElementMap<TElements extends keyof HTMLElementTagNameMap> {
  [elem: TElements]: HTMLElement;
}

interface SpyProxy<T> {
  [field: keyof T]: Function;
}

as index signature parameter types.
Much of this functionality also exists in mapped types, except they had trouble shuffling around symbol information since there was no kind of index signature a symbol could map to. Unlike mapped types, however, since these are just index signatures, they can be added into any object type and in multiples.

Fixes #26470
Fixes #1863 (caveat: well-known symbols are not currently checked as part of a symbol index signature! Such checking should fall out from removing the concept of well-known symbols and using unique symbols instead)
Fixes #2491 in the original sense (rather than with mapped types).

Related: #28315

weswigham added some commits Aug 24, 2018

@weswigham

This comment has been minimized.

Copy link
Member Author

commented Aug 30, 2018

@typescript-bot test this

@typescript-bot

This comment has been minimized.

Copy link
Collaborator

commented Aug 30, 2018

Heya @weswigham, I've started to run the extended test suite on this PR at 2c47d31. You can monitor the build here. It should now contribute to this PR's status checks.

tommy20061222 pushed a commit to tommy20061222/safe-mock that referenced this pull request Oct 28, 2018

tru
(1) upgrade typescript to 3.1.3
(2) typescript 2.9 not allow symbol to be used as index. microsoft/TypeScript#1863. Current there is a PR open to address this issue microsoft/TypeScript#26797
@jhpratt

This comment has been minimized.

Copy link

commented Nov 22, 2018

@RyanCavanaugh I see you marked this for TS 3.2. Is that still the plan?

@DanielRosenwasser DanielRosenwasser removed this from the TypeScript 3.2 milestone Nov 29, 2018

@groege

This comment has been minimized.

Copy link

commented Apr 4, 2019

Hey! This would really help with my project, so much that I would actually use an unstable version for it.
Is there anyway to use this pull already in visual studio and webpack?
I would like something like this: (adapted from the kendo ui):

class ObservableObject<T = any> extends Observable {
        constructor(value?: T);
        [property in keyof T]: T[property];
        uid: string;
        init(value?: any): void;
        get<U extends keyof T>(name: U): T[U];
        parent(): ObservableObject;
        set<U extends keyof T>(name: U, value: T[U];
        toJSON(): Object;
}

This would really clean up my projects and before I start with the next, this would really help.

@Misaka-0x447f

This comment has been minimized.

Copy link

commented Apr 22, 2019

This will implements #2491

@jcalz jcalz referenced this pull request Apr 29, 2019

@RyanCavanaugh RyanCavanaugh assigned weswigham and unassigned weswigham Apr 30, 2019

@IllusionMH IllusionMH referenced this pull request May 5, 2019
5 of 5 tasks complete
@lucienbertin lucienbertin referenced this pull request May 22, 2019
@isiahmeadows isiahmeadows referenced this pull request Jul 27, 2019
9 of 9 tasks complete
@vtulin vtulin referenced this pull request Aug 6, 2019
4 of 5 tasks complete
@dragomirtitian dragomirtitian referenced this pull request Aug 19, 2019
5 of 5 tasks complete
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.