'Typescript: validate type after using type assertion

Is there a way to allow strict type checking on a value after having used a type assertion?

For example, sometimes I need to construct a function type that has attributes:

type FuncPlus = {
    (): void;
    anAttr: number;
    anotherAttr: Array<number>;
};

const f = (() => { }) as FuncPlus;
f.anAttr = 1;

f.anotherAttr.length # will error because `anotherAttr` is `undefined`

I want a clean way of constructing that still gives real type safety.

This is the closest that I've found, but it's not very "typescript-y":

const f: FuncPlus = Object.assign(
    () => { },
    {
        anAttr: 1,
        // without `anotherAttr` defined here, 
        // typescript will throw a compilation error, as desired
    }
)

Does anyone know another way?



Solution 1:[1]

Object.assign is the way FuncPlus type should be assigned, it's already concise enough.

If there is small set of properties, helper function can be used to skip property names:

const getFuncPlus = (fn: () => void, anAttr: number, anotherAttr: number[]): FuncPlus =>
 Object.assign(fn, { anAttr, anotherAttr });

getFuncPlus(() => {}, 1) // causes an error

Solution 2:[2]

Would

type FuncPlus = {
  (): void;
  anAttr: undefined | number;
  anotherAttr: undefined | Array<number>;
};

satisfy your conditions?

const f = (() => {}) as FuncPlus;

f.anAttr = 1;
f.anAttr.toFixed() // good because of the above assignment

f.anotherAttr = "a" // errors - "a" is not assignable to type
f.anotherAttr.length // still errors // f.anotherAttr may be undefined

f.anotherAttr = []
f.anotherAttr.length // good

Solution 3:[3]

Try

((f || {}).anotherAttr || {}).length

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 Estus Flask
Solution 2 Tyler Sebastian
Solution 3 benson23