'Argument of type {stringVariable} not assignable to object of type {string literal}
I have the following code, which takes an options parameter:
const getCat = function (options: { format: "decimal" }) {
return null
}
const options = { format: "decimal" }
const cat = getCat(options)
However, the const cat = getCat(options) runs into an error:
Argument of type '{ format: string; }' is not assignable to parameter of type '{ format: "decimal"; }'.
Types of property 'format' are incompatible.
Type 'string' is not assignable to type '"decimal"'.
How can I cast my options to be of the type TypeScript is looking for?
Solution 1:[1]
You have 2 choices:
Send the options right to the function:
const cat = getCat({ format: "decimal" })Declare a type and have
optionsbe that typetype MyType = { format: "decimal" } const options: MyType = { format: "decimal" } const cat = getCat(options)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | jonrsharpe |
