1

I have defined an interface which goes something like following

export interface Parameter {
   access: string;
   value: string;
}

export interface Parameters {
   parameter: Map<string, Parameter>;
}

I am trying to use the above interface as follows

@Pipe({ name: 'opStatus' })
export class OpStatusPipe {
  transform(parameters: Parameters): any {
    if (parameters.parameter["X1"].value == "true" &&
      parameters.parameter["X2"].value == "true") {
      return "Operational"
    } else {
      return "Not Operational"
    }
  }
}

Now the problem I am facing is that if I use any functions of the Map (set, get, entries, has) then Angular is happy and I can at least run 'ng serve' but then in the browser I keep getting error parameter property does not have function. And if I change those functions to JavaScript style (e.g. parameter["x"].access = "text" , parameters["x"] + "sometext" then Angular serve fails sometimes (Element implicitly has an 'any' type because type 'Map<string, Parameter>' has no index signature. Did you mean to call 'Parameter.get') but browser works always.

I tried to searched everywhere but didn't get any solution. Help is much appreciated.

Angular CLI: 11.0.1
Node: 14.15.0
OS: win32 x64
13
  • What version of JavaScript are you compiling to? In other words, what's your target in tsconfig.json? Also, what browser are you testing in? Commented Nov 17, 2020 at 13:51
  • the value is "target": "es2015", i am using Chrome Version 86.0.4240.198 Commented Nov 17, 2020 at 13:54
  • Your interface definition does not fit the example usage. For example parameters.parameter["X1"] == "true" doesn't make sense when parameters.parameter["X1"] is according to your interface an object with the properties access and value. Commented Nov 17, 2020 at 13:56
  • 1
    Also did you verify that parameters actually is a Map object? Your interface only defines what it should be, not what it is at runtime. Commented Nov 17, 2020 at 14:02
  • 1
    This is the code that consumes data from the pipe ; can you add the code that feeds data into that pipe ? Chances are it doesn't send values typed as you expect. Commented Nov 17, 2020 at 14:05

1 Answer 1

1

You likely want to have a dictionary-like type instead of a Map:

interface Dictionary<T> {
    [Key: string]: T;
}

export interface Parameter {
    access: string;
    value: string;
}

export interface Parameters {
   parameter: Dictionary<Parameter>;
}

This defines the parameter property of a Parameters object as a dictionary where the keys are strings and the values are of the Parameter type.

Sign up to request clarification or add additional context in comments.

2 Comments

This has solved my problem. After so many comments, this one actually helped me. People often don't realize that not everyone has absolute knowledge or understanding of everything. Thank you so much!
@vk-code You are very welcome. Glad that it solved your issue.

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.