0

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"}';
4
  • 1
    export interface IMapping { [key: string]: number; } Commented Apr 4, 2019 at 10:53
  • [key: string] is not special is just any prop that is string (it's dynamic key from ES6 + type - where key is variable not value), you don't use the same for value. Commented Apr 4, 2019 at 10:56
  • The error probably is because your code have var a: IMapping[] = ["foo", {"ABANDONED VEH": 20,...}]; or something like this. Commented Apr 4, 2019 at 11:07
  • The problem you have is that you use JSON string, you need to parse the string to get the object using JSON.parse (that probably just return any). Commented Apr 4, 2019 at 11:09

3 Answers 3

1

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
  },
];
Sign up to request clarification or add additional context in comments.

Comments

1

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.

4 Comments

@Rajesh, I don't see what you mean, can you explain ?
Check my answer. Just explained it there
@Rajesh, the error message is not about a property assignment but about assigning a string directly to an object typed to this interface (const obj: IMapping = 'string')
I did not think about that... Nice. I'll wait for OP to clarify and would remove my answer if this is the case
0

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

8 Comments

If you have [key"...]: {...} you cannot assign a primitive value
I change for it and stills
Do have any way to do this? @Rajesh
right, I adapted with the hint from @Rajesh: I did not realize that you expected complex objects as values in the object - thanks!
@Rajesh Correct, but being picky my edit was done before that answer - and I'd still keep my answer for the short explanation of why the version with any in the question would not work.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.