ArkTS Constraints on TypeScript Features
Use class Instead of Types with Call Signatures
- Rule: arkts-no-call-signatures
Severity: Error
Description: ArkTS does not support call signatures in object types. Instead of using a type with a call signature, define a class with an invoke method.
TypeScript Example:
type DescribableFunction = {
description: string;
(someArg: string): string; // call signature
};
function doSomething(fn: DescribableFunction): void {
console.log(fn.description + " returned " + fn(""));
}
- ArkTS Example:
class DescribableFunction {
description: string;
constructor() {
this.description = "desc";
}
public invoke(someArg: string): string {
return someArg;
}
}
function doSomething(fn: DescribableFunction): void {
console.log(fn.description + " returned " + fn.invoke(""));
}
doSomething(new DescribableFunction());
Use class Instead of Types with Construct Signatures
- Rule: arkts-no-ctor-signatures-type
Severity: Error
Description: ArkTS does not support construct signatures in object types. Instead of using a type with a construct signature, define a class.
TypeScript Example:
class SomeObject {}
type SomeConstructor = {
new (s: string): SomeObject;
};
function fn(ctor: SomeConstructor) {
return new ctor("hello");
}
- ArkTS Example:
class SomeObject {
public f: string;
constructor(s: string) {
this.f = s;
}
}
function fn(s: string): SomeObject {
return new SomeObject(s);
}
Only One Static Block Allowed
- Rule: arkts-no-multiple-static-blocks
Severity: Error
Description: ArkTS does not allow multiple static blocks in a class. Merge multiple static blocks into a single one.
TypeScript Example:
class C {
static s: string;
static {
C.s = "aa";
}
static {
C.s = C.s + "bb";
}
}
- ArkTS Example:
class C {
static s: string;
static {
C.s = "aa";
C.s = C.s + "bb";
}
}
No Support for Index Signatures
- Rule: arkts-no-indexed-signatures
Severity: Error
Description: ArkTS does not support index signatures. Use arrays instead.
TypeScript Example:
interface StringArray {
[index: number]: string;
}
function getStringArray(): StringArray {
return ["a", "b", "c"];
}
const myArray: StringArray = getStringArray();
const secondItem = myArray[1];
- ArkTS Example:
class X {
public f: string[] = [];
}
const myArray: X = new X();
myArray.f.push("a", "b", "c");
const secondItem = myArray.f[1];
Use Inheritance Instead of Intersection Types
- Rule: arkts-no-intersection-types
Severity: Error
Description: ArkTS does not support intersection types. Use inheritance instead.
TypeScript Example:
interface Identity {
id: number;
name: string;
}
interface Contact {
email: string;
phoneNumber: string;
}
type Employee = Identity & Contact;
- ArkTS Example:
interface Identity {
id: number;
name: string;
}
interface Contact {
email: string;
phoneNumber: string;
}
interface Employee extends Identity, Contact {}
No Support for this Type
- Rule: arkts-no-typing-with-this
Severity: Error
Description: ArkTS does not support the this type. Use explicit concrete types instead.
TypeScript Example:
interface ListItem {
getHead(): this;
}
class C {
n: number = 0;
m(c: this) {
// ...
}
}
- ArkTS Example:
interface ListItem {
getHead(): ListItem;
}
class C {
n: number = 0;
m(c: C) {
// ...
}
}
Top comments (0)