TypeScript 中的':' 和'?:'的區別


在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"} ) );

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM