Skip to content
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

Closed
normalser opened this issue Sep 28, 2016 · 5 comments
Closed

Specifying object literal types on the fly #11216

normalser opened this issue Sep 28, 2016 · 5 comments
Labels
Duplicate An existing issue was already created

Comments

@normalser
Copy link

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:

interface A {
    a: string
}

const b = {
    c: {
        a: 'test'
    }:A   // b would be of type { c: A }
}


function test(d:string) {
    switch(d) {
        case 'anotherTest': return {a: 'test'}:A  // function would return type A
    }
}

Right now the closest I can get is to use type assertion

const b = {
    c: {
        a: 'test'
    } as A
}

but it will allow to provide more fields than necessary and I would want TS to throw an error if someone provides more fields

@kengorab
Copy link

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'}
    }
}

@RyanCavanaugh
Copy link
Member

Same as #7481 ?

@normalser
Copy link
Author

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

@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

@normalser
Copy link
Author

That's it @RyanCavanaugh - thanks for pointing it out - tried to find something similar - but sometimes it's difficult

@normalser
Copy link
Author

{c: 'test'}:A or {c: 'test'} is A would be awesome, but of course not sure if it has some other problems

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Sep 28, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants