TypeScript官方文档中接口的可选属性在方法后面为什么又加上了一个json对象?

题目描述

TypeScript官方文档中接口的可选属性在方法后面为什么又加上了一个json对象?

题目来源及自己的思路

具体位置:可选属性

相关代码

这是文档中给的代码

interface SquareConfig { color?: string; width?: number; } 

function createSquare(config: SquareConfig): {color: string; area: number} { 
    let newSquare = {color: "white", area: 100}; 
    if (config.color) { 
        newSquare.color = config.color; 
    } 
    if (config.width) {
        newSquare.area = config.width \* config.width;
    } 
    return newSquare;
    } 
    
let mySquare = createSquare({color: "black"});

你期待的结果是什么?实际看到的错误信息又是什么?

我不明白的是
function createSquare(config: SquareConfig)后面为什么还有一个json对象: {color: string; area: number},他的作用是什么?

阅读 2k
1 个回答

这个是返回值类型,返回值是一个对象,有两个属性,color 跟 area。

推荐问题