美文网首页
typescript 接口传递任意参数

typescript 接口传递任意参数

作者: 小李不小 | 来源:发表于2021-07-14 11:07 被阅读0次

任意属性

有时候我们希望一个接口允许有任意的属性,可以使用如下方式:

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

相关文章

网友评论

      本文标题:typescript 接口传递任意参数

      本文链接:https://www.haomeiwen.com/subject/wgaepltx.html