13af6ab5fSopenharmony_ci# Only ``as T`` syntax is supported for type casts 23af6ab5fSopenharmony_ci 33af6ab5fSopenharmony_ciRule ``arkts-as-casts`` 43af6ab5fSopenharmony_ci 53af6ab5fSopenharmony_ci**Severity: error** 63af6ab5fSopenharmony_ci 73af6ab5fSopenharmony_ciArkTS supports the keyword ``as`` as the only syntax for type casts. 83af6ab5fSopenharmony_ciIncorrect cast causes a compile-time error or runtime ``ClassCastError``. 93af6ab5fSopenharmony_ci``<type>`` syntax for type casts is not supported. 103af6ab5fSopenharmony_ci 113af6ab5fSopenharmony_ciUse the expression ``new ...`` instead of ``as`` if a **primitive** type 123af6ab5fSopenharmony_ci(e.g., a ``number`` or a ``boolean``) must be cast to the reference type. 133af6ab5fSopenharmony_ci 143af6ab5fSopenharmony_ci 153af6ab5fSopenharmony_ci## TypeScript 163af6ab5fSopenharmony_ci 173af6ab5fSopenharmony_ci 183af6ab5fSopenharmony_ci``` 193af6ab5fSopenharmony_ci 203af6ab5fSopenharmony_ci class Shape {} 213af6ab5fSopenharmony_ci class Circle extends Shape {x: number = 5} 223af6ab5fSopenharmony_ci class Square extends Shape {y: string = "a"} 233af6ab5fSopenharmony_ci 243af6ab5fSopenharmony_ci function createShape(): Shape { 253af6ab5fSopenharmony_ci return new Circle() 263af6ab5fSopenharmony_ci } 273af6ab5fSopenharmony_ci 283af6ab5fSopenharmony_ci let c1 = <Circle> createShape() 293af6ab5fSopenharmony_ci 303af6ab5fSopenharmony_ci let c2 = createShape() as Circle 313af6ab5fSopenharmony_ci 323af6ab5fSopenharmony_ci // No report is provided during compilation 333af6ab5fSopenharmony_ci // nor during runtime if cast is wrong: 343af6ab5fSopenharmony_ci let c3 = createShape() as Square 353af6ab5fSopenharmony_ci console.log(c3.y) // undefined 363af6ab5fSopenharmony_ci 373af6ab5fSopenharmony_ci // Important corner case for casting primitives to the boxed counterparts: 383af6ab5fSopenharmony_ci // The left operand is not properly boxed here in in runtime 393af6ab5fSopenharmony_ci // because "as" has no runtime effect in TypeScript 403af6ab5fSopenharmony_ci let e1 = (5.0 as Number) instanceof Number // false 413af6ab5fSopenharmony_ci 423af6ab5fSopenharmony_ci // Number object is created and instanceof works as expected: 433af6ab5fSopenharmony_ci let e2 = (new Number(5.0)) instanceof Number // true 443af6ab5fSopenharmony_ci 453af6ab5fSopenharmony_ci``` 463af6ab5fSopenharmony_ci 473af6ab5fSopenharmony_ci## ArkTS 483af6ab5fSopenharmony_ci 493af6ab5fSopenharmony_ci 503af6ab5fSopenharmony_ci``` 513af6ab5fSopenharmony_ci 523af6ab5fSopenharmony_ci class Shape {} 533af6ab5fSopenharmony_ci class Circle extends Shape {x: number = 5} 543af6ab5fSopenharmony_ci class Square extends Shape {y: string = "a"} 553af6ab5fSopenharmony_ci 563af6ab5fSopenharmony_ci function createShape(): Shape { 573af6ab5fSopenharmony_ci return new Circle() 583af6ab5fSopenharmony_ci } 593af6ab5fSopenharmony_ci 603af6ab5fSopenharmony_ci let c2 = createShape() as Circle 613af6ab5fSopenharmony_ci 623af6ab5fSopenharmony_ci // ClassCastError during runtime is thrown: 633af6ab5fSopenharmony_ci let c3 = createShape() as Square 643af6ab5fSopenharmony_ci 653af6ab5fSopenharmony_ci // Number object is created and instanceof works as expected: 663af6ab5fSopenharmony_ci let e2 = (new Number(5.0)) instanceof Number // true 673af6ab5fSopenharmony_ci 683af6ab5fSopenharmony_ci``` 693af6ab5fSopenharmony_ci 703af6ab5fSopenharmony_ci 71