1

I have a .ts file and I want to use the following type definition:

type MyType = {
    status: "ACTIVE" | "INACTIVE"
}

When I use this type for a variable which I directly set value, everything is good:

const myObj1: MyType = {
    status: "ACTIVE"
}

But if I use a variable to set the value I get an error:

const data = {
    status: "ACTIVE"
}
const myObj2: MyType = data

Error:

Type '{ status: string; }' is not assignable to type 'MyType'.
  Types of property 'status' are incompatible.
    Type 'string' is not assignable to type '"ACTIVE" | "INACTIVE"'.

You can try it on here

I tried different options (like this), but the problem is that my types are generated from Json Schemas and I can't edit the generated files.

  • 1
    The compiler's telling you the correct thing - without further information, the inferred type of `data` is `{ status: string }` - the object is mutable and a different string could be assigned. You could do e.g. `const data = { status: "ACTIVE" } as const;` or even `const data = { status: "ACTIVE" as const };` to tell the compiler that shouldn't change and get the narrower type `{ status: "ACTIVE" }`, which is compatible with `MyType`. But then you might as well just have `const data: MyType = { ... }`. – jonrsharpe Sep 27 '22 at 09:23
  • 1
    Does this answer your question? [Typescript Type 'string' is not assignable to type](https://stackoverflow.com/questions/37978528/typescript-type-string-is-not-assignable-to-type) – jonrsharpe Sep 27 '22 at 09:24
  • Thank you for your comments. @jonrsharpe the question and the answers are similar but the problem is not exactly the same. – György Réti Sep 27 '22 at 11:11

1 Answers1

1

This is because status in data is infered as a string whereas MyType expects it to be either "ACTIVE" or "INACTIVE". You need just to use as const assertion

type MyType = {
    status: "ACTIVE" | "INACTIVE"
}


const myObj1: MyType = {
    status: "ACTIVE"
}


const data = {
    status: "ACTIVE"
} as const
const myObj2: MyType = data

Or you can use new satisfies keyword which is available in TS 4.9

type MyType = {
    status: "ACTIVE" | "INACTIVE"
}

const myObj1: MyType = {
    status: "ACTIVE"
}

const data = {
    status: "ACTIVE"
} satisfies MyType

Playground

  • Thank you, It works fine. For me the first one is better, because the VS Code does not know the `satisfies` keyword yet. – György Réti Sep 27 '22 at 11:04
  • Correct: The first one is NOT OK if I use arrays. But if I use `as MyType` instead of `as const` it works well: [Playground](https://www.typescriptlang.org/play?ts=4.9.0-dev.20220921#code/C4TwDgpgBAsiAq5oF4oG8BQVtQM7AENgBXXALigCIBBAYXgEkA1AUUqgB8qGA5Ox1pSw4AxgQBO5PMHEBLAHYBzANoBdDAF8MGAPQ6A8gGkMIgPbz8UALYh9AIwBWFOIkhRUmHNKKkKNesxsADTC2GKSFMqUAEIEFgAWEBCUQVS0iRCE8ZTqWroGxmYWwFAAJkQEAIzu6KHeJFL+AsF14VJRsQlJKWkZWTmaeESyuABmshC4sAhIJuaWNvYOlc4zbqjlhJXaeiwASnv6e3PFZRUATDWeOPg+jfyBKa0S7TFxuBk9lOlJ-blQBCmRXwJwWtkc51WrhQZ0I5x2BVBJU2BAAzFc6rcGn4HoIQl42pE3l1kqlvn0CNl-oDptCkdZwQ5UVCkDUUaiMEA). – György Réti Sep 27 '22 at 12:36
  • Please keep in mind that using `as MyType` in your last example is not safe. See [this](https://tsplay.dev/mx3PXN) – captain-yossarian from Ukraine Sep 28 '22 at 06:14