Object Properties
TypeScript โฉ
Optional
็ถๅฎฃๅ x?: T ๆ๏ผx ็ๅๅฅๅฏฆ้ไธๆฏ T | undefined
//
interface PaintOptions {
x?: number; // โญ๏ธ optional
y?: number;
}
/*
interface Shape {
paint(opts: PaintOptions = {});
}
*/
const shape = getShape();
shape.paint({ });
shape.paint({ x: 100 });
shape.paint({ y: 100 });
shape.paint({ x: 100, y: 100 });readonly
readonly properties can be modified via aliasingโ๏ธ
Index Signatures
An index signature property type T must be either โstringโ or โnumberโ.
It is possible to support both types of indexers, but the type returned from a numeric indexer must be a subtype of the type returned from the string indexer. This is because when indexing with a number, JavaScript will actually convert that to a string before indexing into an object. That means:
a[100] === a["100"]
Last updated
Was this helpful?