How to specify a typed object literal in TypeScript?

Is there a way to apply type annotation to an object literal directly? Directly, I mean without having to assign it to a variable that is annotated by type. For example: I know I can do it like this:

export interface IBaseInfo {} export interface IMyInfo extends IBaseInfo { name: string; } function testA(): IBaseInfo = { var result: IMyInfo = { name: 'Hey!' }; return result; } 

I can also do it like this:

 function testB(): IBaseInfo = { return { name: 'Hey!' }; } 

But I need something like:

 function testC(): IBaseInfo = { return { name: 'Hey!' }: IMyInfo; // <--- doesn't work } 

Or like this:

 function testD(): IBaseInfo = { return IMyInfo: { name: 'Hey!' }; // <--- doesn't work } 
+6
source share
4 answers

The answer is to use the identification function:

 function to<a>(value: a): a { return value; } const instance = to<SomeInterface>({ prop1: 'text', prop2: 123 }); 
+6
source

Intellisense for members of object literals is provided by the contextual type (see section 4.19 of the specification) of the expression.

You can get the context type in various ways. Some of the most common places where the context type is applied:

  • Variable initializer with type annotation
  • An expression in a return expression in a function or getter with return type annotation
  • Expression in a statement of type type ( <T>expr )

In your example, you can use a type statement to force an object literal to have a context type:

 function testB() { return <IMyInfo>{ name: 'Hey!' }; } 
+15
source

Remember that these interfaces follow the duck set: if an object is like an interface, it corresponds to the interface.

So,

 function testB(): IBaseInfo = { return { name: 'Hey!' }; } 

exactly matches

 function testA(): IBaseInfo = { var result: IMyInfo = { name: 'Hey!' }; return result; } 

In any case, the returned object looks like IMyInfo, so this is IMyInfo. Nothing that happens inside a function affects the interfaces that it corresponds to.

However, in your examples, the return value of the function is IBaseInfo, so the compiler and intellisense will assume that the object is only IBaseInfo. if you want the caller to know that the return value is IMyInfo, you need to make the return value of the IMyInfo function:

 function testB(): IMyInfo = { return { name: 'Hey!' }; } 

or using type inference, just

 function testB() = { return { name: 'Hey!' }; } 
+2
source

Your first and second examples fail, so you cannot do this. :)

Pretty sure you don't need to specify any types in your literal. As long as your literal interface satisfies the requirements of the interface, you are good.

 interface IMyInfo { name: string; } var asdf = { name: "test" } var qwer: IMyInfo = asdf; 

If you want intellisense, you need to do something like:

enter image description here

Or maybe this is what you are looking for. Intellisense works here, at least in the playground.

enter image description here

Or maybe this. :)

enter image description here

0
source

Source: https://habr.com/ru/post/956268/


All Articles