目录

Compatibility Warning Suppression

Since DevEco Studio 6.1.0 Beta2, the ArkTS API compatibility warning suppression capability is added to DevEco Studio. You can add the @SuppressWarnings annotation or @SuppressWarnings single-line comment to clear COMPATIBILITY warnings.

Suppressing by Annotation

You can add the @SuppressWarnings({ rules: [SuppressWarningsType.COMPATIBILITY] }) annotation to variable declarations, type declarations, function declarations, namespace declarations, annotation declarations, struct members, class members, and API members to clear internal or self-generated warnings.

Example

import { Available, SuppressWarnings, SuppressWarningsType } from '@ohos.annotation';

// Mark the minimum available version as API 23. If the compatible version of the project is earlier than API 23, a warning is triggered.
@Available({ minApiVersion: "23" })
function test(): string {
  return "hello world";
}

// Use the @SuppressWarnings annotation to suppress warnings in class members.
@SuppressWarnings({ rules: [SuppressWarningsType.COMPATIBILITY] })
class TestClass {
  a = test(); // The warning will be suppressed.

  testFunction() {
    test(); // The warning will be suppressed.
  }
}

Suppressing by Single-Line Comment

If annotations cannot be added, you can add the @SuppressWarnings compatibility single-line comment above the code line where the warning needs to be suppressed.

Example

import { Available } from '@ohos.annotation';

// Mark the minimum available version as API 23. If the compatible version of the project is earlier than API 23, a warning is triggered.
@Available({ minApiVersion: "23" })
function test(): string {
  return "hello world";
}

// Use the @SuppressWarnings comment to suppress warnings for non-declarative elements.
class TestClass {
  testFunction() {
    // @SuppressWarnings compatibility
    test(); // The warning will be suppressed.
  }
}