TypeScript 中的':' 和'?:'的区别
<div>在rect中, 有下面的代码: </div><div> </div>
<div>export type ReactComponentLike =</div>
<div> | string</div>
<div> | ((props: any, context?: any) => any)</div>
<div> | (new (props: any, context?: any) => any);</div>
<div> </div>
<div>props: any, 使用的是: </div>
<div>context?:any , 却使用的是?: </div>
<div> </div>
<div>这两个符号有什么区别呢?</div>
<div> </div>
<div><span style="background-color: rgba(255, 102, 0, 1)">:这两个都表示参数的类型, 不同的是, 一个参数构造函数必须有的, 而另外一个, 是可选的.</span></div>
<div> </div>
<div>看一下下面的代码:</div>
<div> </div>
<div>//sharp.ts</div>
<div>
<p>interface Shape {<br> name: string;<br> width: number;<br> height: number;<br> color?: string;<br>}<br> <br>function area(shape : Shape) {<br> var area = shape.width * shape.height;<br> return "I'm " + shape.name + " with area " + area + " cm squared";<br>}<br> <br>console.log( area( {name: "rectangle", width: 30, height: 15} ) );<br>console.log( area( {name: "square", width: 30, height: 30, color: "blue"} ) );</p>
<p> </p>
</div><br><br>
来源:https://www.cnblogs.com/montai/p/13356990.html
頁:
[1]