一键导入
strict-typescript
A reference for the repository's strict type rules, explaining forbidden practices and how to correctly handle types and mocking in tests.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
A reference for the repository's strict type rules, explaining forbidden practices and how to correctly handle types and mocking in tests.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
Explains how to write and run tests in the jj-view repository, detailing the test suites, their purposes, and how to filter test runs down to single test cases. You must read this before editing or debuggins tests.
Generate release notes for a new version
基于 SOC 职业分类
| name | strict_typescript |
| description | A reference for the repository's strict type rules, explaining forbidden practices and how to correctly handle types and mocking in tests. |
This document outlines the strict TypeScript rules enforced in the jj-view repository and provides guidance on how to correctly cast types, narrow types, and mock dependencies without resorting to anti-patterns.
The project has strict type-checking enabled ("strict": true in tsconfig.json). To maintain code quality and reliability, the following practices are strictly forbidden:
any types: The use of any disables type checking and is completely forbidden. Use strict types, or if the type is truly unknown, use unknown.// @ts-ignore// eslint-disable-line (for type-related lint rules)// @ts-expect-errorunknown and then immediately casting to another type.
const myVar = foo as unknown as Bar;When you encounter a situation where the TypeScript compiler is unhappy, you must solve it using proper type-safe methods.
Instead of forcing a cast, use type guards (e.g., typeof, instanceof, or custom type guard logic) to narrow unknown or union types into the specific type you need.
// BAD (Forbidden)
const data = getSomeUnknownData();
const myString = data as unknown as string;
const length = myString.length;
// GOOD (Type Narrowing)
const data = getSomeUnknownData();
if (typeof data === 'string') {
// TypeScript now knows `data` is a string
const length = data.length;
} else {
// Handle the unexpected type
throw new Error('Expected a string');
}
If you have an object from an external source typed as unknown and need to access its properties, define an interface for the expected structure and build a type guard, or use safe assertion functions.
The most common reason developers reach for double-casting (as unknown as Type) is when creating mock objects for tests. This is forbidden.
To create partial mock objects that satisfy a required interface, use the createMock utility (where available) or structurally type what you need.
vscodeWhen unit testing, you often need to mock the vscode module. Do not attempt to cast custom objects as typeof vscode. Instead, use the dedicated createVscodeMock helper provided in the test suite.
// src/test/my-test.test.ts
import { vi } from 'vitest';
// GOOD: Use the provided mock factory and vitest dynamic import
vi.mock('vscode', async () => {
const { createVscodeMock } = await import('../vscode-mock');
return createVscodeMock({
// Provide partial overrides here
window: {
showInformationMessage: vi.fn(),
showErrorMessage: vi.fn(),
},
workspace: {
// Only provide what the test needs
workspaceFolders: [{ uri: { fsPath: '/custom/test/path' } }],
},
});
});
The createVscodeMock function handles the heavy lifting of satisfying the vscode module shapes without requiring forbidden casts. Always refer to src/test/vscode-mock.ts to see what is already mocked by default.