- Learning TypeScript 2.x
- Remo H. Jansen
- 82字
- 2025-04-04 17:02:05
Weak types
A weak type is an object literal type in which all properties are optional:
interface User {
name?: string;
age?: number;
}
TypeScript allows us to add a value with some or all the properties defined in the weak type, but it doesn't allow us to assign properties that are not part of the weak type:
let user1: User = { name: "Remo", age: 28 }; // OK
let user2: User = { firstName: "Remo", yearBorn: 28 }; // Error