MCPcopy Create free account
hub / github.com/SincereCSL/Playgrounds / area

Function area

TypeScript/StudyTypeScript/src/advanced_01.ts:58–69  ·  view source on GitHub ↗
(s: Shape)

Source from the content-addressed store, hash-verified

56
57type Shape = Square | Rectangle | Circle
58function area(s: Shape){ // 利用两种类型的共有属性来创建不同的类型保护区块
59 switch (s.kind){
60 case "square":
61 return s.size * s.size;
62 case "rectangle":
63 return s.height * s.width
64 case "circle":
65 return Math.PI * s.r ** 2
66 default:
67 return ((e: never) => {throw new Error(e)})(s)
68 }
69}
70
71console.log(area({kind: 'circle',r: 1}))

Callers 1

advanced_01.tsFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected