Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Type 'string' is not assignable to type '"right" | "inherit" | "left" | "center" | "justify" | undefined'

The same problem is asked in this question but It didn’t answer my question as to why we need to use ‘as’ to make it work:
TS2322 Type 'string' is not assignable to type '"left" | "center" | "right" | undefined'

I have the following code but it’s showing error

Type 'string' is not assignable to type '"right" | "inherit" | "left" | "center" | "justify" | undefined'

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

even though the value is in one of the values allowed.

interface DataTableHeader {
  key: string
  label: string
  align?: "right" | "inherit" | "left" | "center" | "justify"
}

const columns = [
    {
        key: "title",
        label: "Title",
    },
    {
        align: "right",
        key: "costPrice",
        label: "Cost Price", 
    }
]

const DataTable = (columns?: DataTableHeader[]) => {
    return columns
}

DataTable(columns) // here

// Errors in code
Argument of type '({ key: string; label: string; align?: undefined; } | { align: string; key: string; label: string; })[]' is not assignable to parameter of type 'DataTableHeader[]'.
  Type '{ key: string; label: string; align?: undefined; } | { align: string; key: string; label: string; }' is not assignable to type 'DataTableHeader'.
    Type '{ align: string; key: string; label: string; }' is not assignable to type 'DataTableHeader'.
      Types of property 'align' are incompatible.
        Type 'string' is not assignable to type '"right" | "inherit" | "left" | "center" | "justify" | undefined'.

enter image description here

>Solution :

In TypeScript, the type string is not a subtype of "right" | "inherit" | "left" | "center" | "justify" | undefined. This means that a value of type string cannot be assigned to a variable with a type of "right" | "inherit" | "left" | "center" | "justify" | undefined.

To fix this error, you can use a type assertion to explicitly tell the TypeScript compiler that the string value is actually one of the allowed values. This can be done by using the as keyword followed by the type that you want to assert the value to be.

You can update the code like this:

const columns = [
  {
    key: "title",
    label: "Title",
  },
  {
    align: "right" as "right" | "inherit" | "left" | "center" | "justify",
    key: "costPrice",
    label: "Cost Price",
  }
]

Or you can update the type of the align property in the DataTableHeader interface to be of type string, which would allow any string value to be assigned to it. This would avoid the need for a type assertion.

interface DataTableHeader {
  key: string;
  label: string;
  align?: string;
}

const columns = [
  {
    key: 'title',
    label: 'Title'
  },
  {
    align: 'right',
    key: 'costPrice',
    label: 'Cost Price'
  }
];
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading