如何忽略玩笑中的打字稿错误

Ehs*_*har 8 typescript jestjs expo

使用 jest 运行测试时如何忽略一些打字稿错误?

我尝试过使用// @ts-ignore评论,但仍然无法开玩笑。

我正在使用jest-expo内部使用jest@25.0.2

const styles = Stylesheet.create({
   header: {
     // here is the typescript error
     position: "sticky"
   }
})
Run Code Online (Sandbox Code Playgroud)

这里position: "sticky"没有在类型中定义Stylesheet,我想抑制这个错误

Joe*_*app 0

在测试文件中的任何位置调用此实用程序函数。

/**
 * Embeds code that will never run within a callback. Useful for
 * testing expected type errors.
 * @param description Description of the code that will never run
 * @param callback Callback that will never run
 */
export function ignore(_description: string, _code: () => void) {}
Run Code Online (Sandbox Code Playgroud)

我喜欢将它嵌入到相关的describe. 例如:

describe("insertions", () => {

  // ...

  ignore("insertOne() type errors", () => {
    // @ts-expect-error - inserted object must have all required columns
    userTable.insertOne({});
    // @ts-expect-error - inserted object must have all required columns
    userTable.insertOne({ email: "xyz@pdq.xyz" });
    // @ts-expect-error - returning argument can't be a string
    userTable.insertOne(USERS[0], "id");
    // @ts-expect-error - returning argument can't be a string
    userTable.insertOne(USERS[0], "*");
    // @ts-expect-error - returning arguments must be valid column names
    userTable.insertOne(USERS[0], [""]);
    // @ts-expect-error - returning arguments must be valid column names
    userTable.insertOne(USERS[0], ["notThere"]);
    // @ts-expect-error - returning arguments must be valid column names
    userTable.insertOne(USERS[0], ["notThere", "*"]);
  });
});
Run Code Online (Sandbox Code Playgroud)

类型“测试”由编译器在编译时完成。

您可能需要添加此 linter 规则才能ignore()编译:

'@typescript-eslint/no-empty-function': 'off'
Run Code Online (Sandbox Code Playgroud)