-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Specifying object literal types on the fly #11216
Comments
No new syntax is needed for this. You could do const b: { c: A } = { // <- Note the type annotation on b
c: {
a: text'
}
} and function test(d:string): A { // <- Note the type annotation on the function
switch(d) {
case 'anotherTest': return {a: 'test'}
}
} |
Same as #7481 ? |
@kengorab Thanks for answer but maybe my example shouldn't be as simple as I showed. I'm talking about building objects that are deeply nested and function that has multiple cases in switch statement const z: {
y: {
x: {
w: {
v: {
c: A
}
}
}
}
} = { y: { x: { w: { v: {c} } } } } vs const z = { y: { x: { w: { v: { c }:A } } } } and for functions with multiple case statements i would need to define const and then return const |
That's it @RyanCavanaugh - thanks for pointing it out - tried to find something similar - but sometimes it's difficult |
|
Sometimes it would be handful to be able to specify type of object literal on the fly (without creating const / variable) with exact type definition
Hopefully it's not violating 'no new syntax' rule (too much)
So something like that:
Right now the closest I can get is to use type assertion
but it will allow to provide more fields than necessary and I would want TS to throw an error if someone provides more fields
The text was updated successfully, but these errors were encountered: