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
If there is one, what is the proper way to combine rt.Dictionary and rt.Record such that the resulting static type looks like this:
type Test {
someKey: number,
[key: string]: string
}
Basically, what I want is a record type that has some keys that we know of, and their values have specific types, but it also can have other keys, but their will be of another specific type :). (Sort of "every person has the same fields and we use custom types to type their values, but each person can have some extra fields, and the values
rt.Dictionary().And(rt.Record()) and rt.Record().And(rt.Dictionary()) seem to kinda override each other, ie. the dictionary seems to want to always take precedence and enforce the values in the record, even though the record specified completely different types for the values.
I wonder if this would require some kind of Symbol for rt.Record, such that I don't even have to use the dictionary. Something like rt.Record({rt.Default}: rt.String) (rt.Default would be a symbol).
Currently we have no option for this AFAIK (I can't believe this hasn't been an issue until 2021 🤣) We need something like this, absolutely! Thanks for pointing out.
One thing to say, the type you provided is invalid at least in TS 4.2.3:
typeTest={someKey: number,[key: string]: string}// Property 'someKey' of type 'number' is not assignable to string index type 'string'.
But you can:
typeTest={someKey: number,[key: string]: unknown}
So I'd mark this as an upstream matter, at least partially: microsoft/TypeScript#7765, microsoft/TypeScript#17867. I'm not sure it would eventually be supported in TS or not, but anyway, we'd support this preserving forward compatibility for the upstream solution...
I think your proposal is the correct way to achieve this. Let's call the symbol Rest, then the API would look like this:
Record({[Rest]: Unknown})// Without any function call, the key type will be String by default Record({[Rest(Symbol)]: Unknown})// With a function call, you can specify some type available as key type in the current TS
Ah yes. Actually, I have written in as type, but the place where I got it from had it as an interface :D, eg.
interface Test {
someKey: number,
[key: string]: string
}
And that does not raise any errors. I know there's subtle differences between types and interfaces, and seems like this is one of them. Not sure why, though :)
No, as noticed in microsoft/TypeScript#7765 (comment), that won't work too. Also see microsoft/TypeScript#7765 (comment), you can define a type without error by intersecting, but it can't be used properly. This seems to be a very restriction of the current type system. Hope it will be resolved...
Hopefully that will change soon. For now, I'll have to type the dictionary value with all the types that are contained within the record (as well as the type(s) that I expect the actual rest property to have), as I should do even when using the plain types/interfaces from TS, as you pointed out :)
Activity
yuhr commentedon Mar 26, 2021
Currently we have no option for this AFAIK (I can't believe this hasn't been an issue until 2021 🤣) We need something like this, absolutely! Thanks for pointing out.
One thing to say, the type you provided is invalid at least in TS 4.2.3:
But you can:
So I'd mark this as an upstream matter, at least partially: microsoft/TypeScript#7765, microsoft/TypeScript#17867. I'm not sure it would eventually be supported in TS or not, but anyway, we'd support this preserving forward compatibility for the upstream solution...
I think your proposal is the correct way to achieve this. Let's call the symbol
Rest, then the API would look like this:petr-motejlek commentedon Mar 26, 2021
Ah yes. Actually, I have written in as
type, but the place where I got it from had it as an interface :D, eg.And that does not raise any errors. I know there's subtle differences between types and interfaces, and seems like this is one of them. Not sure why, though :)
yuhr commentedon Mar 26, 2021
No, as noticed in microsoft/TypeScript#7765 (comment), that won't work too. Also see microsoft/TypeScript#7765 (comment), you can define a type without error by intersecting, but it can't be used properly. This seems to be a very restriction of the current type system. Hope it will be resolved...
yuhr commentedon Mar 28, 2021
Hmm, unfortunately it seems pretty hard to work with symbols and rest properties in current versions of TS...
new symbolas the return type microsoft/TypeScript#37469[-]How to combine Dictionary and Record[/-][+]How to represent rest properties in `Record`[/+]petr-motejlek commentedon Mar 29, 2021
Hopefully that will change soon. For now, I'll have to type the dictionary value with all the types that are contained within the record (as well as the type(s) that I expect the actual rest property to have), as I should do even when using the plain types/interfaces from TS, as you pointed out :)
ivands commentedon Dec 4, 2023
Also need this feature
[-]How to represent rest properties in `Record`[/-][+]How to represent rest properties in `Object`[/+]