vitest/prefer-expect-type-of Style
作用
强制使用 expectTypeOf 而不是 expect(typeof ...)。
为什么这不好?
与使用 expect(typeof ...) 相比,Vitest 提供了一种更具表现力、类型更安全的方式来测试类型。
示例
以下是此规则的错误代码示例:
js
test('type checking', () => {
expect(typeof 'hello').toBe('string')
expect(typeof 42).toBe('number')
expect(typeof true).toBe('boolean')
expect(typeof {}).toBe('object')
expect(typeof () => {}).toBe('function')
expect(typeof Symbol()).toBe('symbol')
expect(typeof 123n).toBe('bigint')
expect(typeof undefined).toBe('undefined')
})以下是此规则的正确代码示例:
js
test("type checking", () => {
expectTypeOf("hello").toBeString();
expectTypeOf(42).toBeNumber();
expectTypeOf(true).toBeBoolean();
expectTypeOf({}).toBeObject();
expectTypeOf(() => {}).toBeFunction();
expectTypeOf(Symbol()).toBeSymbol();
expectTypeOf(123n).toBeBigInt();
expectTypeOf(undefined).toBeUndefined();
});如何使用
To enable this rule using the config file or in the CLI, you can use:
json
{
"plugins": ["vitest"],
"rules": {
"vitest/prefer-expect-type-of": "error"
}
}ts
import { defineConfig } from "oxlint";
export default defineConfig({
plugins: ["vitest"],
rules: {
"vitest/prefer-expect-type-of": "error",
},
});bash
oxlint --deny vitest/prefer-expect-type-of --vitest-plugin版本
此规则已于 v1.44.0 中添加。
