简体   繁体   中英

How can I create interface in Typescript for array

I want to create an interface in typescript to receive this values:

{
  "ABANDONED VEH": 4,
  "ADMINISTRATION": 4,
  "ALARM-BUS/RES": 4
}

When I try to use the correct interface that I create, I'm getting the error:

Type string is not assignable to IMapping

What I'd already tried:

export interface IMapping {
  [key: string]: { value: string };
}

export interface IMapping {
  key: any;
}

[key: string]: string;

PS. Here's my real code:

file interface.ts:

export interface IMapping {
  [key: string]: number;
}

file that use this interface:

import { IMapping } from '../interfaces';
const mapping: IMapping =  '{'
  + '"ABANDONED VEH": 4,'
  + '"ADMINISTRATION": 4"}';

According to the shape of your data, the value type in your interface should be number and not { value: string } :

export interface IMapping {
  [key: string]: number;
}

Or if you want to specify the properties:

export interface IMapping {
  'ABANDONED VEH': number,
  'ADMINISTRATION': number,
  'ALARM-BUS/RES': number
}

However, according to your error message and your code, you are assigning a string to an object of type IMapping which is invalid.

Type string is not assignable to IMapping

const mapping: IMapping =  '{'
  + '"ABANDONED VEH": 4,'
  + '"ADMINISTRATION": 4"}';
//                      ^ remove this and use JSON.parse()

If you know the object at compile-time, then use a literal expression to define it:

const mapping: IMapping = {
  "ABANDONED VEH": 4,
  "ADMINISTRATION": 4,
  "ALARM-BUS/RES": 4
}

If your data comes from an API in a string format, then use JSON.parse() on it:

const mapping: IMapping = JSON.parse('{"ABANDONED VEH": 4,"ADMINISTRATION": 4,"ALARM-BUS/RES": 4}');

Note however that your string is not valid JSON, you have a trailing " after the last 4 .

This looks like the typing for your data.

interface IMapping {
  [key: string]: number;
}

const data: IMapping = {
  "ABANDONED VEH": 4,
  "ADMINISTRATION": 4,
  "ALARM-BUS/RES": 4
};

however, is not clear why you're talking about arrays (indexeed ones), in that case you might want a list of IMapping

const list: IMappings[] = [
  {
    "ABANDONED VEH": 4,
    "ADMINISTRATION": 4,
    "ALARM-BUS/RES": 4
  },
  {
    "ABANDONED VEH": 5,
    "ADMINISTRATION": 5,
    "ALARM-BUS/RES": 5
  },
];

Your values are numbers, not strings. You should try:

export interface IMapping {
  [key: string]: number;
}

Your second version with the any type will not work because you did not use the brackets - that will expect a property with name key , not a dictionary-like object.

EDIT :

After your edit, it is obvious that you are trying to assign a string. You should assign the object directly:

const mapping: IMapping =  {
  "ABANDONED VEH": 4,
  "ADMINISTRATION": 4};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM