Not all properties are required
interface Amount {
value: string;
precision?: number;
}The properties can be modified only at object creation.
interface Point {
readonly x: number;
readonly y: number;
}Object literals undergo excess property checking when
interface SquareConfig {
color?: string;
width?: number;
}
function createSquare(config: SquareConfig): { color: string; area: number } {
}
// error: 'colour' not expected in type 'SquareConfig'
let mySquare = createSquare({ colour: "red", width: 100 });Avoid the checks
let mySquare = createSquare({ width: 100, opacity: 0.5 } as SquareConfig);Can be used to describe functions
interface SearchFunc {
(source: string, subString: string): boolean;
}interface NumberDictionary {
[index: string]: number;
length: number; // ok, length is a number
name: string; // error, the type of 'name' is not a subtype of the indexer
}Index signatures can be readonly
interface ReadonlyStringArray {
readonly [index: number]: string;
}
let myArray: ReadonlyStringArray = ["Alice", "Bob"];
myArray[2] = "Mallory"; // error!