查看: 109|回覆: 0

TypeScript 中的':' 和'?:'的区别

[複製鏈接]

5

主題

0

回帖

0

積分

热心网友

金币
0
閲讀權限
220
精華
0
威望
0
贡献
0
在線時間
0 小時
註冊時間
2009-8-25
發表於 2020-7-21 19:31:00 | 顯示全部樓層 |閲讀模式
在rect中, 有下面的代码: 
 
export type ReactComponentLike =
    | string
    | ((props: any, context?: any) => any)
    | (new (props: any, context?: any) => any);
 
props: any, 使用的是: 
context?:any , 却使用的是?: 
 
这两个符号有什么区别呢?
 
:这两个都表示参数的类型, 不同的是, 一个参数构造函数必须有的, 而另外一个, 是可选的.
 
看一下下面的代码:
 
//sharp.ts

interface Shape {
name: string;
width: number;
height: number;
color?: string;
}

function area(shape : Shape) {
var area = shape.width * shape.height;
return "I'm " + shape.name + " with area " + area + " cm squared";
}

console.log( area( {name: "rectangle", width: 30, height: 15} ) );
console.log( area( {name: "square", width: 30, height: 30, color: "blue"} ) );

 



来源:https://www.cnblogs.com/montai/p/13356990.html
回覆

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 立即注册

本版積分規則

相关侵权、举报、投诉及建议等,请发 E-mail:qiongdian@foxmail.com

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

在本版发帖返回顶部