You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Quick fix for 'unions can't be used in index signatures, use a mapped object type instead' #24220
Nobody knows what mapped object types are, so let's give them a quick fix that
+1, just came here because I was expecting 2.9 to support unions as index signatures per your example code. I think this has been a long desired feature: #5683, #16760, etc..
Though Bar has no index signature (i.e., you can't then do (obj as Bar)[value as Foo]).
Edit: Though if you could make the caveat a non-issue, I'd be eternally grateful!
Reacted by Lukas Elmer, WCarrollSTO, johntran, Carlos González, Joel Malone, Thibault HENRY, eeglbalazs, Mathieu M-Gosselin, Gábor IMRE, lizc and 959 more
Reacted by Ahmad Alfy, Roberto Sousa, Roberto Guerra and Sage Starr
Reacted by Yoomin, Abraham Schilling, sumbad, Beck, simmmm, Daniel Christopher, xaun, Ezell, Giampaolo Bellavite, Darren Hickling and 40 more
Reacted by Maksim Ovsyannikov, codelovesme, Albert Vallverdu, Sung M. Kim, lu-zen, Alberto Fernandez, Martin Nemček, Kazuma Ebina, Jonathan Gruber, Andrei and 148 more
Reacted by Juan M, H2RO, Neal Granger, Chris Applegate, Kyle Kashuba, Gotchi, Abraham Schilling, gilm123, Beck, Artha and 17 more
Reacted by G, isbasex, Chris, Dawson Botsford, Aymeric Robini, Mariano Pardo, Jacob Elder, bro, florian-ah, Sebastián Balay and 135 more
Reacted by Evan Bacon, Sung M. Kim, Andrei, alvinkeegangoh, Rami Chasygov, dekhtiarenko, Karim, G, Aleksei Klimenko, isbasex and 82 more
Reacted by G, Kyle, Paul Miu, Vladyslav Yazykov, Juan M, H2RO, Roman Šimík, Maksym Lysenko, Felipe Acosta, Onur Yıldırım and 23 more
Bartek (@apieceofbart) thanks for the suggestion, I've looked into it, a bit redundant in terms of interfaces but can work, I'm not sure if I'll implement it now, since it's not a big deal if the final user tries a bitwise condition with two operators, mongo will throw an error anyway
I'm trying to keep the mongo-operators definitions as simple as possible to avoid me headaches 😁 maybe in future a proper support is added
Michael Vasin (@mvasin) FWIW, this appears to achieve the same result, but I agree entirely that it should be a feature of interfaces just as it is on types.
type Foo = 'a' | 'b';
type Bar = {
[key in Foo]: any
}
interface A extends Bar { }
class Wol implements A{
a: any;
b: any;
}
Why exactly can't an index signature use an enum type? The mapped type almost does what I want, but then TypeScript expects every string from the enum to exist as a defined key. I don't actually want to assert that every key exists, more that if any keys do exist, they must live in the enum.
For example for the type:
typeMyType={[Key: 'foo'|'bar'|'zip']: number;};
This should satisfy:
constx: MyType={foo: 1,zip: 2};
While I could just set the other keys undefined for a mapped type, I prefer to make the keys optional, but if they're present, the value cannot be undefined. If I make the mapped type values optional the code works but the types are less strong.
Reacted by Mikkel Jakobsen, Simon Beaulieu, Ashlynne Mitchell, Spike Brehm, Romain Fièvé, Timo Taglieber, Ben Hodgson, gundam-wing, Erik Lilja, Jon Surrell and 3 more
I can scroll up and figure out a workaround, but just wanted to provide feedback that this issue happens frequently in day to day work in slightly different scenarios.
Reacted by Caleb Taylor, Lemmingh, eaubin and John Wilson
typeMapKey=string|number;typeObjectLiteral<TextendsMapKey,V=any>={[PinTextendsnumber ? string : T]: V;};exportconstmapToObjectLiteral=<TextendsMapKey,V>(map: Map<T,V>)=>Array.from(map).reduce((objLit,[key,value])=>{objLit[keyaskeyofObjectLiteral<T>]=value;returnobjLit;},{}asObjectLiteral<T,V>);// how to make a better type of map ?constm=newMap<1|"foo","a"|"b">();m.set(1,"a");m.set("foo","b");consto=mapToObjectLiteral(newMap(m));console.log(o[1],o.foo);// just got an union type of every member of 'o'
Activity
dannycochran commentedon May 18, 2018
+1, just came here because I was expecting 2.9 to support unions as index signatures per your example code. I think this has been a long desired feature: #5683, #16760, etc..
mattbasta commentedon May 18, 2018
You can do this:
Though
Barhas no index signature (i.e., you can't then do(obj as Bar)[value as Foo]).Edit: Though if you could make the caveat a non-issue, I'd be eternally grateful!
Kingwl commentedon May 20, 2018
i'd like to work on this 😆
Kingwl commentedon May 21, 2018
what should we do if containing object type is an class?
I can only imagine that it is an interface
so what should follow code do after quickfix?
mhegazy commentedon May 21, 2018
I would say this should not be fixable..
dannycochran commentedon Jul 18, 2018
Mohamed Hegazy (@mhegazy) I'm using 3.0.0-rc and still getting the same error as originally posted. Is this expected?
mhegazy commentedon Jul 18, 2018
yes. the error is correct. this issue was tracking adding a quick fix for it, that is the light pulp next to the error message.
18 remaining items
apieceofbart commentedon Dec 24, 2019
Filippo Conti (@b4dnewz) You can't do it in Typescript. Workaround: #10575
benwinding commentedon Dec 24, 2019
Filippo Conti (@b4dnewz), if you only want 1 property, why not do it like this?
b4dnewz commentedon Dec 25, 2019
Ben Winding (@benwinding) unfortunately the returned shape is different from what mongodb expects
Bartek (@apieceofbart) thanks for the suggestion, I've looked into it, a bit redundant in terms of interfaces but can work, I'm not sure if I'll implement it now, since it's not a big deal if the final user tries a bitwise condition with two operators, mongo will throw an error anyway
I'm trying to keep the mongo-operators definitions as simple as possible to avoid me headaches 😁 maybe in future a proper support is added
benwinding commentedon Dec 25, 2019
Filippo Conti (@b4dnewz) fair enough,
Perhaps a simpler option you might be able to use is:
That's about the closest you'll get without too much duplication
apieceofbart commentedon Jan 2, 2020
This will not yield error in this example:
I thought that's the whole point
benwinding commentedon Jan 3, 2020
Bartek (@apieceofbart),
Woah! that's weird 😮 I did not know that!
It's seems that Typescript doesn't support mutually exclusive types for objects. It's also was proposal for the language here: #14094
It is still technically possible though...
From this stackoverflow answer this is possible to achieve this using conditional types (the hardest types), but it aint pretty....
If anyone could make this prettier or better, please do
daverickdunn commentedon Jan 13, 2020
Michael Vasin (@mvasin) FWIW, this appears to achieve the same result, but I agree entirely that it should be a feature of interfaces just as it is on types.
chriszrc commentedon Jan 17, 2020
For typescript 3.5, it seems like I have to do this:
Is this the best way to do this?
benwiley4000 commentedon Mar 5, 2020
Why exactly can't an index signature use an enum type? The mapped type almost does what I want, but then TypeScript expects every string from the enum to exist as a defined key. I don't actually want to assert that every key exists, more that if any keys do exist, they must live in the enum.
For example for the type:
This should satisfy:
While I could just set the other keys undefined for a mapped type, I prefer to make the keys optional, but if they're present, the value cannot be undefined. If I make the mapped type values optional the code works but the types are less strong.
defusioner commentedon Mar 18, 2020
Thanks!
Useful when you need to define a type that partially matches a dictionary
ruojianll commentedon Apr 13, 2020
https://www.typescriptlang.org/docs/handbook/advanced-types.html#mapped-types
ZYinMD commentedon Aug 2, 2020
"Partial" can be used on Records too:
breck7 commentedon Oct 16, 2020
I find myself unwittingly visiting this GitHub page every month or so.
My latest one is a real simple one:
I can scroll up and figure out a workaround, but just wanted to provide feedback that this issue happens frequently in day to day work in slightly different scenarios.
iahu commentedon Oct 22, 2020
here is an example:
nelson6e65 commentedon Nov 1, 2020
#24220 (comment)
Very useful. Thanks! 🚀