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?
getCat({ format: "decimal" })or declare an actual type for the parameter.optionsis infered as{ format: string }unless you addas const(either to the object or the string) [related: stackoverflow.com/a/62652353/3001761].{ format: "decimal" }and not just any string. If you declare a type you can also doconst options: MyType = /* ... */to get the same effect.