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
This is not an easy change as it would require some deeper architectural reworking. Note that you can accomplish pretty much the same effect with the intersection type operator:
Just wanted to add: I have considered suggesting this myself, though one thing I must mention is that even if it was made possible to inherit from computed types, that would still miss a significant use case for them as generators of "templates" for new types (rather than serving as base types). I'll try to demonstrate:
Let's say I have an existing type of this shape:
typeSomeType={a: number;b: boolean;c: string;}
Now I want to declare a type that has the same exact keys only each key is now mapped to a an object that has a property val with the type of the corresponding key in SomeType. Mapped types allow me to do that:
Now I want to customize the resulting type, so the closest thing the language provides me is to "inherit" from that mapped type. Let's assume that was possible:
I forgot to specify property c, so it "fell back" to { val: string }, however the compiler didn't tell me anything went wrong! why? because I used the wrong operator.
What if what I want is not to "inherit" from the mapped type, but simply use it as a "model" or "constraint" for the new type?
I have tried my best to propose a solution in #12942. It's still in an early stage though. I've submitted it at least with the hope that it might influence or inspire the typescript team to include something of that sort in the future, even if it eventually appears with a different syntax or in a more limited form.
I am trying to develop Vue.js app with TypeScript and also having a trouble because of the limitation that mapped types are not available in classes or interfaces.
In Vue.js, you can pass props to a component (just like React) and the properties of props are injected into the properties of the component instance.
Therefore, the declaration of the class of Vue component should be something like this, which is not possible currently.
declareclassVue<Props>{[PinkeyofProps]: Props[P];}// example usageclassFooextendsVue<{message: string}>{render(h){return<span>{this.message}</span>;}}
Activity
ahejlsberg commentedon Dec 16, 2016
This is not an easy change as it would require some deeper architectural reworking. Note that you can accomplish pretty much the same effect with the intersection type operator:
rotemdan commentedon Dec 16, 2016
Just wanted to add: I have considered suggesting this myself, though one thing I must mention is that even if it was made possible to inherit from computed types, that would still miss a significant use case for them as generators of "templates" for new types (rather than serving as base types). I'll try to demonstrate:
Let's say I have an existing type of this shape:
Now I want to declare a type that has the same exact keys only each key is now mapped to a an object that has a property
valwith the type of the corresponding key inSomeType. Mapped types allow me to do that:Now I want to customize the resulting type, so the closest thing the language provides me is to "inherit" from that mapped type. Let's assume that was possible:
There's only one problem here:
I forgot to specify property
c, so it "fell back" to{ val: string }, however the compiler didn't tell me anything went wrong! why? because I used the wrong operator.What if what I want is not to "inherit" from the mapped type, but simply use it as a "model" or "constraint" for the new type?
I have tried my best to propose a solution in #12942. It's still in an early stage though. I've submitted it at least with the hope that it might influence or inspire the typescript team to include something of that sort in the future, even if it eventually appears with a different syntax or in a more limited form.
kimamula commentedon Dec 30, 2016
I am trying to develop Vue.js app with TypeScript and also having a trouble because of the limitation that mapped types are not available in classes or interfaces.
In Vue.js, you can pass props to a component (just like React) and the properties of props are injected into the properties of the component instance.
Therefore, the declaration of the class of Vue component should be something like this, which is not possible currently.
Anders Hejlsberg (@ahejlsberg)'s workaround does not work in this situation.