Home > Software design >  Argument of type <Interface> is not assignable to parameter of type 'never'
Argument of type <Interface> is not assignable to parameter of type 'never'

Time:03-21

I just started out doing some typescripts in angular and came across a problem that I wish for some assistance of.

list = [];
interface a {
   s : string;
   n : number;
}

const b = {'asdf',1234} as a;
list.push(b)

But I keep receiving the error

Argument of type a is not assignable to parameter of type 'never'.

Any suggestions on what I can do?

CodePudding user response:

You have to specify the property name as below:

const b = { s: 'asdf', n: 1234 } as a;

Sample Typescript Playground

CodePudding user response:

You forgot to add the key names. Interfaces and objects do not rely on the position of the arguments:

const list = [];

interface A {
    s: string;
    n: number;
}

const b: A = {s: 'asdf', n: 1234};
list.push(b)

I've added missing const for the list declaration. Keep in mind that it is a good practice to name types starting with capital letters. This way you can easier distinguish between variables and types/interfaces. There is also an alternative syntax with type A declared near the const b.

  • Related