Sub topic 6
Weak type detection
This chapter assumes you know TypeScript interfaces and optional properties, and that you have read the previous chapters on structural typing and excess property checks. It covers one narrow rule: the check that rejects assignments to types whose properties are all optional.
The problem weak type detection solves
Under structural typing, a source type is assignable to a target type if it has every property the target requires. Now consider a target where nothing is required. Every object has all of its zero required properties, so every object would be assignable.
interface Options {
color?: string;
width?: number;
}Without an extra rule, a value like { colour: "red" } would be accepted as Options. The spelling mistake would go unnoticed, and the option would be silently ignored at runtime. Weak type detection exists to catch this case.
What counts as a weak type
A weak type is an object type that meets all of these conditions:
- It has at least one property.
- Every property is optional.
- It has no index signatures.
- It has no call or construct signatures.
Options above is weak. The following types are not:
| Type | Weak? | Reason |
|---|---|---|
| `{ color?: string }` | Yes | One property, all optional |
| `{ color?: string; id: number }` | No | `id` is required |
| `{}` | No | No properties at all |
| `{ [key: string]: string }` | No | Has an index signature |
| `{ color?: string; (): void }` | No | Has a call signature |
The rule
When the target is a weak type, TypeScript requires the source to share at least one property with it. If the source has properties but none of them appear in the target, the assignment fails.
interface Options {
color?: string;
width?: number;
}
const input = { colour: "red" };
const opts: Options = input; // Error: no properties in common with weak type 'Options'The error message names the problem directly: the types have no properties in common. Note that input is a variable, not a fresh object literal. Excess property checks only apply to fresh literals, so they would not catch this. Weak type detection fills that gap.
The rule is about overlap, not exact matching. A source that shares one property and has extra properties passes:
interface Options {
color?: string;
width?: number;
}
const input = { color: "red", extra: true };
const opts: Options = input; // OK: 'color' is sharedHere the extra property is allowed because input is not a fresh literal. If you wrote the object literal directly in the assignment, the excess property check would reject extra.
How it relates to excess property checks
The two checks overlap but are independent.
| Row | Excess property check | Weak type detection |
|---|---|---|
| Applies to | Fresh object literals only | Any source type |
| Target | Any object type | Weak types only |
| Fails when | Source has a property the target lacks | Source shares no property with the target |
| Example failure | `{ x: 1, z: 3 }` to `Point` | `{ colour: "red" }` to `Options` |
A fresh literal assigned to a weak type can trigger either check. With const o: Options = { colour: "red" }, TypeScript reports the excess property error first, because the literal check runs on the literal itself.
Edge cases
Empty sources pass
A source with no properties does not fail the rule. The check applies when the source has at least one property and none match.
interface Options {
color?: string;
width?: number;
}
const empty = {};
const opts: Options = empty; // OK: nothing to conflictPrimitives and other non-object sources
A primitive is checked through its apparent type, such as String for string. Since String has properties like length and none match Options, the assignment fails.
interface Options {
color?: string;
}
const s: Options = "red"; // Error: Type '"red"' has no properties in common with type 'Options'Functions
A function type also has properties from Function, such as name and length. Assigning a function to a weak type usually fails unless the weak type has a matching property.
interface Callbacks {
onDone?: () => void;
}
const fn = () => {};
const cb: Callbacks = fn; // Error: no properties in common with weak type 'Callbacks'This catches a common mistake, where a function is passed in place of an options object.
Unions and intersections
For a union target, the source needs to be assignable to at least one member. If a member is weak, the weak check applies to that member only. For an intersection target, the source must be assignable to every member. TypeScript applies the weak check to the intersection as a whole, so Options & { id: number } is not weak because id is required.
Generic and mapped types
Partial<T> produces a weak type when T has properties. That means Partial<Point> rejects a source with no overlapping properties:
interface Point {
x: number;
y: number;
}
const patch = { z: 3 };
const p: Partial<Point> = patch; // Error: no properties in common with weak type 'Partial<Point>'Working around the check
If the rejection is correct, fix the source. If the source really is unrelated and you intend the assignment, you have three options.
- Add a shared property to the source, even an optional one.
- Add an index signature to the target, which makes it no longer weak:
[key: string]: unknown. - Use a type assertion, which bypasses the check when the types are sufficiently related. See the later chapter on assertions and comparability.
Prefer the first option. The error usually points to a real mistake.
Why the check is limited
The rule only fires when there is zero overlap. A source that shares one misspelled-free property and also contains a misspelled one passes, because TypeScript cannot tell an intentional extra property from a typo. Weak type detection is a narrow safety net, not a full typo detector.
Exercise
Given interface Config { debug?: boolean; level?: number }, which of these assignments fail, and which check rejects each? (1) const a: Config = { debug: true, mode: "x" }; (2) const v = { mode: "x" }; const b: Config = v; (3) const w = { debug: true, mode: "x" }; const c: Config = w;
Show answer
(1) fails because of the excess property check: the object is a fresh literal and mode does not exist in Config. (2) fails because of weak type detection: v is not a fresh literal, but Config is weak and v shares no property with it. (3) compiles: w is not fresh, so there is no excess property check, and it shares debug with Config, so weak type detection passes.
Exercise
Why does const x: { id?: string } = () => {} fail, while const y: { id?: string; [k: string]: unknown } = () => {} can compile?
Show answer
The first target is a weak type, and a function shares no property with it, so weak type detection rejects it. The second target has an index signature, so it is not weak and the check does not apply.