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?
Object.assign(...).