任意属性
有时候我们希望一个接口允许有任意的属性,可以使用如下方式:
interface SquareConfig {
color?: string;
width?: number;
[propName: string]: any;
}
//[propName: string]: any; 传递任意属性,可以传递多个属性和方法
function createSquare(config: SquareConfig): { color: string; area: number } {
let newSquare = {
color:"white",
area:100
}
console.log('----------------')
console.log(config.colour)
console.log(config.colours)
return newSquare;
}
let mySquare = createSquare({ colour: "red",colours:"green", width: 100 } as SquareConfig);
image.png
网友评论