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