Skip to content

TypeScript

基于 typescript-eslint 推荐配置,仅作用于 .ts / .tsx 文件。

js
import lintspec from '@qwqo/eslint-config'

export default lintspec({ typescript: true })

传入对象可开启需要类型信息的规则(依赖 tsconfig.json,能力更强但更慢):

js
export default lintspec({ typescript: { typeAware: true } })

标记说明:🔧 可通过 eslint --fix 自动修复 · 💡 在编辑器中提供修复建议。

一致的类型导入(consistent-type-imports) 🔧

类型导入约定使用顶层 import type {...}

不推荐
ts
import { User } from './types'

const user: User = load()
推荐
ts
import type { User } from './types'

const user: User = load()

顶层 import type(no-import-type-side-effects) 🔧

整条导入仅含类型时,约定改为顶层 import type,不使用内联 type 限定符。

不推荐
ts
import { type User } from './types'
推荐
ts
import type { User } from './types'

命名约定(naming-convention)

类、接口、类型别名、枚举等类型名称约定使用 PascalCase(不以小写开头)。

不推荐
ts
class widget {}
推荐
ts
class Widget {}

禁止显式 any(no-explicit-any) 🔧 💡

约定避免显式 any

不推荐
ts
const value: any = fetchData()
推荐
ts
const value: unknown = fetchData()

禁止空对象类型(no-empty-object-type) 💡

约定避免意义不明的 {} 类型。

不推荐
ts
type Props = {}
推荐
ts
type Props = Record<string, never>

限制 @ts 注释(ban-ts-comment) 💡

约定用带说明的 @ts-expect-error 取代裸 @ts-ignore

不推荐
ts
// @ts-ignore
const x = unsafe()
推荐
ts
// @ts-expect-error 第三方类型缺失,已确认运行时安全
const x = unsafe()

禁止 require(no-require-imports)

约定使用 ESM import 取代 require。考虑到渐进迁移,该规则为警告(warn)而非错误,且不支持自动修复。

不推荐
ts
const fs = require('node:fs')
推荐
ts
import fs from 'node:fs'

禁止 namespace(no-namespace)

约定使用 ES 模块取代 TypeScript namespace

不推荐
ts
export namespace Utils {
  export const version = 1
}
推荐
ts
export const version = 1