Fix type X is not assignable to type Y
Best PracticesThe error message that undefined cannot be assigned to a specific type often occurs when working with optional parameters. When a parameter is marked as optional, it can either be undefined
or of the specified type. In TypeScript this is represented by a union type such as undefined | string
. If you want to use a function that doesn’t accept undefined
as a parameter (e.g., parseInt
), you must first validate that your value exists.
Here are some helpful tricks for accomplishing this.
Continue reading →