I have a somewhat basic question that I can't seem to figure out on my own, so I'm hoping to get some help with it.
type SweetAlertPosition =
'top' | 'top-start' | 'top-end' | 'top-left' | 'top-right' |
'center' | 'center-start' | 'center-end' | 'center-left' | 'center-right' |
'bottom' | 'bottom-start' | 'bottom-end' | 'bottom-left' | 'bottom-right';
Here's a simplified version of the main interface:
interface SweetAlertOptions {
...,
position?: SweetAlertPosition
}
class Swal {
mixin(options: SweetAlertOptions)
}
When I explicitly pass options to the .mixin call like this: Swal.mixin({ position: 'bottom' }), there are no errors. But if I define options in a const object like this:
defaultOptions = {
position: 'bottom'
}
Passing it now produces an error - Swal.mixin(defauiltOptions)
Types of property 'position' are incompatible.
Type 'string' is not assignable to type '"top" | "top-start" | "top-end" | "top-left" | "top-right" | "center" | "center-start" | "center-end" | "center-left" | "center-right" | "bottom" | "bottom-start" | "bottom-end" | "bottom-left" | "bottom-right" | undefined'.
I've learned that I need to cast the String type to const like this:
defaultOptions = {
position: 'botton' as const | as SweetAlertPosition
}
But why does passing options via a const object (defaultOptions) make the position property become a String-type, whereas passing it directly does not? O_o