1# Structural typing is not supported 2 3Rule ``arkts-no-structural-typing`` 4 5**Severity: error** 6 7Currently, ArkTS does not support structural typing, i.e., the compiler 8cannot compare public APIs of two types and decide whether such types are 9identical. Use other mechanisms (inheritance, interfaces or type aliases) 10instead. 11 12 13## TypeScript 14 15 16``` 17 18 interface I1 { 19 f(): string 20 } 21 22 interface I2 { // I2 is structurally equivalent to I1 23 f(): string 24 } 25 26 class X { 27 n: number = 0 28 s: string = "" 29 } 30 31 class Y { // Y is structurally equivalent to X 32 n: number = 0 33 s: string = "" 34 } 35 36 let x = new X() 37 let y = new Y() 38 39 console.log("Assign X to Y") 40 y = x 41 42 console.log("Assign Y to X") 43 x = y 44 45 function foo(x: X) { 46 console.log(x.n, x.s) 47 } 48 49 // X and Y are equivalent because their public API is equivalent. 50 // Thus the second call is allowed: 51 foo(new X()) 52 foo(new Y()) 53 54``` 55 56## ArkTS 57 58 59``` 60 61 interface I1 { 62 f(): string 63 } 64 65 type I2 = I1 // I2 is an alias for I1 66 67 class B { 68 n: number = 0 69 s: string = "" 70 } 71 72 // D is derived from B, which explicitly set subtype / supertype relations: 73 class D extends B { 74 constructor() { 75 super() 76 } 77 } 78 79 let b = new B() 80 let d = new D() 81 82 console.log("Assign D to B") 83 b = d // ok, B is the superclass of D 84 85 // An attempt to assign b to d will result in a compile-time error: 86 // d = b 87 88 interface Common { 89 n: number 90 s: string 91 } 92 93 // X implements interface Z, which makes relation between X and Y explicit. 94 class X implements Common { 95 n: number = 0 96 s: string = "" 97 } 98 99 // Y implements interface Z, which makes relation between X and Y explicit. 100 class Y implements Common { 101 n: number = 0 102 s: string = "" 103 } 104 105 let x: Common = new X() 106 let y: Common = new Y() 107 108 console.log("Assign X to Y") 109 y = x // ok, both are of the same type 110 111 console.log("Assign Y to X") 112 x = y // ok, both are of the same type 113 114 function foo(c: Common): void { 115 console.log(c.n, c.s) 116 } 117 118 // X and Y implement the same interface, thus both calls are allowed: 119 foo(new X()) 120 foo(new Y()) 121 122``` 123 124 125