Type Inference
This page explains the aspects of TypeScript type inference that commonly affect type tests.
Type Widening
In a mutable context a literal type is inferred as its base type:
import { } from "tstyche";
const = { : 2, : [3, 3] };
()..<{ : number; : number[] }>();To prevent widening, use type assertions :
import { } from "tstyche";
const = { : 6 as , : [9, 9] as 9[] };
()..<{ : 6; : 9[] }>();Using the const type assertion on the entire object is also possible, but it creates a different type than individual assertions would:
import { } from "tstyche";
const = { : 6, : [9, 9] } as ;
()..<{ readonly : 6; readonly : readonly [9, 9] }>();Excess Property Checks
When passed as arguments, object literals undergo excess property checks :
import { } from "tstyche";
let : { ?: number } = {};
= { silent: true, : 800 };
// Same as above, object literal may only specify known properties
({ : true, : 800 })...<{ ?: number }>();
<{ ?: number }>()...({ : true, : 800 });
// But object types are allowed to have excess properties
<{ : true; : 800 }>()..<{ ?: number }>();
<{ ?: number }>()..<{ : true; : 800 }>();In most cases, these failures are helpful — they prevent outdated assertions from accumulating in your tests.
If excess property checks are not what you need, see the TypeScript Handbook on how to get around them. For example, pass a reference instead of an object literal:
import { } from "tstyche";
const = { : true, : 800 };
()..<{ ?: number }>();Last updated on