'shortcut for TS `undefined as Type | undefined`

I have a const like this:

const DEFAULT = {
  result: undefined as Result | undefined,
};

The type of result seems too long for me. Is there a way to shorten it?



Solution 1:[1]

You could explicitly declare the type and use the shorthand optional property notation in the type signature. After that, you don't need to explicitly declare the property undefined in the object as that is the default.

type Result = true;

const DEFAULT: { result?: Result } = {};

TypeScript Playground Link

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 sno2