Sub topic 2
Weak Type Detection and Related Assignability Rules
Assume you know TypeScript interfaces, type aliases, functions, and compiler errors. This chapter focuses on how TypeScript compares source and target types.
Weak Type Detection
A weak type is an object type whose properties are all optional. TypeScript rejects an assignment when the source object has no properties in common with that type.
interface Options {
color?: string;
width?: number;
}
const input = { colour: "red" };
const options: Options = input;
// Error: no properties in common with type 'Options'The misspelled colour property does not match any property in Options. This check catches likely mistakes. It does not mean that every extra property is forbidden. A source with one recognized property can still be assignable.
Literal Types and Widening
A literal type contains one exact value, such as "fast" or 42. Mutable variables usually widen literals to broader types.
type Mode = "fast" | "slow";
const fixed = "fast"; // type: "fast"
let widened = "fast"; // type: string
const a: Mode = fixed; // OK
const b: Mode = widened; // ErrorUse as const when an object should preserve its literal types:
const config = { mode: "fast" } as const;
const mode: Mode = config.mode; // OKUnions and Intersections
A union means “one of these types.” A value assigned to a union must be assignable to at least one member. Reading from a union only permits operations common to every member.
An intersection means “all of these types.” A value assigned to an intersection must satisfy every member.
type Id = string | number;
type Named = { name: string } & { id: number };
const id: Id = 10;
const item: Named = { name: "Ada", id: 1 };`any`, `unknown`, `never`, and `void`
anydisables most checking and is assignable to nearly every type.unknownaccepts every value, but must be narrowed before use.neverrepresents impossible values and is assignable to every type.voiddescribes a function whose return value should not be used.
let value: unknown = "text";
// const length: number = value.length; // Error
function fail(): never {
throw new Error("failed");
}
const message: string = fail(); // OKComparing Function Types
A function requiring fewer parameters can replace one requiring more. The caller may provide the extra arguments safely.
const fewer: (a: number, b: number) => void =
(a) => {}; // OK
const more: (a: number) => void =
(a, b: number) => {}; // ErrorReturn types work in the opposite direction: a function may return a more specific type than the target requires.
Variance
Variance describes how generic types change when their type arguments change.
- Covariance: producers can return a narrower type.
- Contravariance: consumers can accept a broader type.
- Invariance: both input and output use require the exact relationship.
With strictFunctionTypes, function properties check parameters contravariantly. Method syntax remains bivariant for compatibility, which can allow unsafe assignments.
Exercise
Why does unknown accept a string but not assign to a string variable?
Show answer
unknown can hold any value, so the compiler requires narrowing before treating it as a specific type.