1

I have an interface as shown below for typing an object.

export interface IList{
  name: string;
  age: number;
  option: number;
  quantity: number;
  priority: number;
}

Due to some requirement I have to assign a "string" to the "priority" property at the end of all operations before sending it to the backend.

As I've to assign a string, I tried using a union operator :-

priority : number | string;

But all the other pieces of code wherever I used other operations taking this as a number into consideration is also throwing me the below error:

Argument of type 'string | number' is not assignable to parameter of type 'number'
  Type 'string' is not assignable to type 'number'.

How do I get around this and use priority as a both a string and number to type my object.

Here is one condition where I am using the "IList" interface as a type and assigning a number if multiGroupHeader is true, else I have to assign a string :-

  public updatePriorities(Lists) {
    if(!this.multiGroupHeader){
    const priorities = Lists.map((list:IList) => list.priority);
    const uniquePriorities = [...new Set(priorities)];
    if (uniquePriorities.length === 1 && uniquePriorities[0] === 1) {
      return;
    }
    uniquePriorities.sort((priority1: number, priority2: number) => priority1 - priority2);
    const updatedPriorities = uniquePriorities.map((priority: number, index: number) => {
      return index + 1;
    });

    uniquePriorities.forEach((id: number, index: number) => {
      Lists.forEach((list: List) => {
        if (list.priority === id) {
          list.priority = updatedPriorities[index];
        }
      });
    });
   } else {
     Lists.forEach((list:List) => list.priority = "CURRENT");
   }
  }
3
  • Check it's type using type guards before you use it? Commented Mar 6, 2020 at 9:21
  • all the other pieces of code will need to be able to get number | string, not just number Commented Mar 6, 2020 at 9:28
  • 1
    Can you not cast it to a string at the point it is sent to the back end, but otherwise treat it as a number? Commented Mar 6, 2020 at 9:28

1 Answer 1

2

The way you have constructed your interface, it should not have any typescript errors unless you're not following the contract of that interface.

I am assuming you are doing something like this:

 interface IList{
     name: string;
     age: number;
     option: number;
     quantity: number;
     priority: number | string;
 }

 class Test {
  constructor() {
    let listItem: IList = {
        name: "John Doe",
        age: 23,
        option: 2,
        quantity: 2,
        priority: 2
     };
    }
 }

This should not give you any errors. To check the type errors of this, you can use TypeScript Playground. There should be no reason for a workaround.

This is the link to the code above: https://www.typescriptlang.org/play/#code/JYOwLgpgTgZghgYwgAgJIBlgGcwG8BQyyIcAthAFzI5SgDmA3IcnHZcQK6kBG0TRAewAOYYAJBUQXXlH7IAjhzjhgYAJ6TpfZkNoDa6zT2jIAPtTC0QjfAF98+BABs4WLMgAqEHMgJEiCOI0HAhg+gAUAJS+zP7+ThBgyE7YYKiQpFQYqcgAvDFxhf4k5FQARABSAgAWIMgAIgIQZQA0sUX+rOwATADMbR2FwqLiVN0Dg-6KyqKGyOPtg7piBhrzi-62ckT29kA

I would recommend you to try it out there and see what are the actual contract errors you're facing.

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

Comments

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.