Sub topic 9
Weak Type Detection in TypeScript
Assume you know basic interfaces, type aliases, object types, and assignments. This chapter focuses on one extra check TypeScript applies to weak types.
A weak type is an object type in which every property is optional. It describes an object that may contain none of the listed properties.
interface PaintOptions {
color?: string;
width?: number;
}PaintOptions is weak because color and width are both optional. An empty object is valid:
const defaults: PaintOptions = {};Why weak types need a special check
Structural typing normally allows extra properties. If a value has all the required properties of a target type, TypeScript accepts it. Weak types have no required properties, so that rule could accept many misspelled objects by accident.
TypeScript therefore checks that the source and weak target share at least one property name:
interface PaintOptions {
color?: string;
width?: number;
}
const valid: PaintOptions = { color: "blue", opacity: 0.5 };
// The extra opacity property is allowed because color is known.
const typo = { colour: "blue" };
// @ts-expect-error: no property is shared with PaintOptions
const invalid: PaintOptions = typo;The error says that the source type has no properties in common with PaintOptions. The problem is not simply that colour is extra. The problem is that none of the source's properties could be an option recognized by the target type.
This check also applies when the source is a variable. That differs from an excess property check, which mainly targets a fresh object literal.
Weak types versus ordinary object types
Adding one required property makes a type non-weak. TypeScript then uses normal structural assignability.
interface RequestOptions {
url: string;
timeout?: number;
}
const source = { retries: 3 };
// @ts-expect-error: url is required
const request: RequestOptions = source;This error is about the missing required url, not weak type detection. The target is not weak because url is required.
A weak type can accept an object with several extra properties as long as at least one property is known:
interface PaintOptions {
color?: string;
width?: number;
}
const mixed = {
color: "red",
opacity: 0.8,
label: "warning"
};
const options: PaintOptions = mixed;The variable mixed is assignable because it has the known color property. The extra properties remain present at runtime, but they are not visible through the PaintOptions type.
Common causes and fixes
Misspelled configuration names are the most common cause:
interface CompilerOptions {
strict?: boolean;
target?: "es2020" | "es2022";
}
const config = {
strcit: true
};
// @ts-expect-error: strcit is not a known option
const options: CompilerOptions = config;Fix the spelling, or change the type if the property is genuinely supported. Do not silence the error with a type assertion unless you have checked the runtime behavior.
An index signature can intentionally allow arbitrary property names:
interface Metadata {
[key: string]: string | undefined;
}
const data: Metadata = {
colour: "blue",
owner: "sam"
};This changes the contract. Use it only when unknown keys are part of the design.
> callout warning > Weak type detection is a compile-time check. JavaScript still allows any properties at runtime. Validate external data separately when values come from files, requests, or users. > ```
A useful prediction rule
When checking an assignment to an object type:
- If the target has a required property, look for missing required members.
- If every target property is optional, check whether the source shares at least one property name.
- If the source is a fresh object literal, also check for excess properties.
- Remember that a type assertion can suppress these checks without changing the runtime value.
Exercise
Why does assigning { colour: "red" } through a variable still fail when the target type is { color?: string; width?: number }?
Show answer
The target is a weak type, and the source shares no property name with it. Weak type detection rejects the assignment to catch likely misspellings.