15-从 TypeScript 到 ArkTS 的适配规则(5)
15-从 TypeScript 到 ArkTS 的适配规则(5)
不支持确定赋值断言
**规则:**arkts-no-definite-assignment
级别:警告
ArkTS不支持确定赋值断言,例如:let v!: T。改为在声明变量的同时为变量赋值。
TypeScript
let x!: number // 提示:在使用前将x初始化
initialize();
function initialize() { x = 10;}
console.log('x = ' + x);
ArkTS
function initialize(): number { return 10;}
let x: number = initialize();
console.log('x = ' + x);
不支持在原型上赋值
**规则:**arkts-no-prototype-assignment
级别:错误
ArkTS没有原型的概念,因此不支持在原型上赋值。此特性不符合静态类型的原则。
TypeScript
let C = function(p) { this.p = p; // 只有在开启noImplicitThis选项时会产生编译时错误}
C.prototype = { m() { console.log(this.p); }}
C.prototype.q = function(r: string) { return this.p == r;}
ArkTS
class C { p: string = '' m() { console.log(this.p); } q(r: string) { return this.p == r; }}
不支持globalThis
**规则:**arkts-no-globalthis
级别:警告
由于ArkTS不支持动态更改对象的布局,因此不支持全局作用域和globalThis。
TypeScript
// 全局文件中var abc = 100;
// 从上面引用'abc'let x = globalThis.abc;
ArkTS
// file1export let abc: number = 100;
// file2import * as M from 'file1'
let x = M.abc;
不支持一些utility类型
**规则:**arkts-no-utility-types
级别:错误
ArkTS仅支持Partial、Required、Readonly和Record,不支持TypeScript中其他的Utility Types。
对于Record类型的对象,通过索引访问到的值的类型是包含undefined的联合类型。
不支持对函数声明属性
**规则:**arkts-no-func-props
级别:错误
由于ArkTS不支持动态改变函数对象布局,因此,不支持对函数声明属性。
不支持Function.apply和Function.call
**规则:**arkts-no-func-apply-call
级别:错误
ArkTS不允许使用标准库函数Function.apply和Function.call。标准库使用这些函数来显式设置被调用函数的this参数。在ArkTS中,this的语义仅限于传统的OOP风格,函数体中禁止使用this。
不支持Function.bind
**规则:**arkts-no-func-bind
级别:警告
ArkTS不允许使用标准库函数Function.bind。标准库使用这些函数来显式设置被调用函数的this参数。在ArkTS中,this的语义仅限于传统的OOP风格,函数体中禁止使用this。
不支持as const断言
**规则:**arkts-no-as-const
级别:错误
ArkTS不支持as const断言。在标准TypeScript中,as const用于标注字面量的相应字面量类型,而ArkTS不支持字面量类型。
TypeScript
// 'hello'类型let x = 'hello' as const;
// 'readonly [10, 20]'类型let y = [10, 20] as const;
// '{ readonly text: 'hello' }'类型let z = { text: 'hello' } as const;
ArkTS
// 'string'类型let x: string = 'hello';
// 'number[]'类型let y: number[] = [10, 20];
class Label { text: string = ''}
// 'Label'类型let z: Label = { text: 'hello'}
不支持导入断言
**规则:**arkts-no-import-assertions
级别:错误
由于在ArkTS中,导入是编译时而非运行时特性,因此,ArkTS不支持导入断言。在运行时检查导入的API是否正确,对于静态类型的语言来说是没有意义的。改用常规的import语法。
TypeScript
import { obj } from 'something.json' assert { type: 'json' }
ArkTS
// 编译时将检查导入T的正确性import { something } from 'module'
限制使用标准库
**规则:**arkts-limited-stdlib
级别:错误
ArkTS不允许使用TypeScript或JavaScript标准库中的某些接口。大部分接口与动态特性有关。ArkTS中禁止使用以下接口:
全局对象的属性和方法:eval
Object:proto、defineGetter、defineSetter、
lookupGetter、lookupSetter、assign、create、
defineProperties、defineProperty、freeze、
fromEntries、getOwnPropertyDescriptor、getOwnPropertyDescriptors、
getOwnPropertySymbols、getPrototypeOf、
hasOwnProperty、is、isExtensible、isFrozen、
isPrototypeOf、isSealed、preventExtensions、
propertyIsEnumerable、seal、setPrototypeOf
Reflect:apply、construct、defineProperty、deleteProperty、
getOwnPropertyDescriptor、getPrototypeOf、
isExtensible、preventExtensions、
setPrototypeOf
Proxy:handler.apply()、handler.construct()、
handler.defineProperty()、handler.deleteProperty()、handler.get()、
handler.getOwnPropertyDescriptor()、handler.getPrototypeOf()、
handler.has()、handler.isExtensible()、handler.ownKeys()、
handler.preventExtensions()、handler.set()、handler.setPrototypeOf()
- 0回答
- 6粉丝
- 1关注
- 11-从TypeScript到ArkTS的适配规则(1)
- 12-从TypeScript到ArkTS的适配规则(2)
- 13-从TypeScript到ArkTS的适配规则(3)
- 14-从TypeScript到ArkTS的适配规则(4)
- 16-从 TypeScript 到 ArkTS 的适配规则(6)
- 从零到一:flutter_timezone库的鸿蒙适配深度探索
- FlutterToast 三方库鸿蒙适配之旅:从零到一的深度实践
- Flutter_udid 三方库鸿蒙适配之旅:从零到一的深度实践
- flutter_exit_app 三方库鸿蒙适配之旅:从零到一的深度实践
- Flutter到鸿蒙的跨越:torch_light库的鸿蒙适配之旅
- (七)ArkCompiler 的编译流程:从源码到字节码的深度解析
- Flutter到鸿蒙的跨越:flutter_native_contact_picker库的鸿蒙适配之旅
- Flutter到鸿蒙的跨越:flutter_native_contact_picker库的鸿蒙适配之旅
- 从零到智能:打造智能灯控应用
- 「Mac畅玩鸿蒙与硬件15」鸿蒙UI组件篇5 - Slider和Progress组件