-
Notifications
You must be signed in to change notification settings - Fork 117
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
Change HashMap
to export mapped types
#339
Conversation
Interesting! How sure are we that
There definitely are some semantic differences. Here are a couple I have found: type A = { [key: string]: number };
let a: A = { "x": 42 };
let x: number = a["x"]; // ok
type B = { [key in string]?: number };
let b: B = { "y": 42 };
let y: number = b["y"]; // error type A = { [key: string]: number };
let a: A = { "x": undefined }; // error
type B = { [key in string]?: number };
let b: B = { "y": undefined }; // ok For just That does mean that this change is breaking, correct? Do we want to treat it as such, or is this more of a bug fix? |
I think this behavior is better because a
This is annoying, and I don't know if there's a way to implement the feature without this behavior.
Yeah, this does look like a breaking change, it'll make TypeScript demand a whole lot of null checks when dealing with |
This breaks iterating over object values: //before
type A = {[key: string]: number};
let a: A = {"a": 1, "b": 2, "c": 3};
//el is a `number` below
let sum = Object.values(a).reduce((acc, el) => acc + el, 0);
//after
type A = {[key: string]?: number};
let a: A = {"a": 1, "b": 2, "c": 3};
//el is a `number|undefined` below
let sum = Object.values(a).reduce((acc, el) => acc + el, 0); This does make sense that record types will return undefined on keys that aren't in them, but I think it's better handled by setting |
Interesting! I do still think that the new representation is better overall, though I'd love to be convinced otherwise. For one, I think we should chose the most sensible bindings with the default tsc config. I'd also err on the side of having false-positive tsc errors instead of false-negatives. |
You can give type A = { [key in string]?: number }
const a: A = { a: 1, b: 2, c: 3 }
Object.values(a).reduce<number>((acc, el = 0) => acc + el, 0) |
Yeah, this trivial case is somewhat easy to work around, but if Is it possible to detect that the key type is enumerable and only output the enum Keys {
A,
B,
C,
}
struct A<T> {
foo: HashMap<Keys, T>
} type A<T> = {
foo: {
"A"?: T,
"B"?: T,
"C"?: T,
}
} This is essentially doing the type mapping by hand. I think this TS bug is related, and if fixed setting |
Goal
Allow enums to be used as
HashMap
keysCloses #338
Changes
Change
HashMap
to export{ [key in K]?: V }
instead of{ [key: K]: V }
Checklist