Copilot instructions for TypeScript code patterns with unknown narrowing, type guards that check field types, exhaustive unions, purposeful generics and satisfies.
.github/instructions/github-copilot-typescript-standards.instructions.md. Downloaded files need to be placed in that repository path.---
applyTo: "**/*"
---
# TypeScript Code Standards
Applies to: TypeScript source files (`**/*.ts`, `**/*.tsx`) when writing or reviewing functions, types and data handling.
Version baseline: checked against TypeScript 5.9 (the `satisfies` operator needs 4.9+). Read `package.json` for the installed version and for a validation library (Zod, Valibot, io-ts) before writing hand-rolled validation.
## Types for objects
- Use `interface` for object shapes that may be extended or implemented, and `type` for unions, intersections and mapped types. Follow the file's existing choice when both would work.
- Model closed sets of values as string-literal unions (`'admin' | 'user'`), not `string`.
- Model states with discriminated unions so that impossible combinations cannot be written.
```ts
type LoadState<T> =
| { status: 'idle' }
| { status: 'loading' }
| { status: 'success'; data: T }
| { status: 'error'; error: Error };
function label(state: LoadState<string>): string {
switch (state.status) {
case 'idle':
return 'Not started';
case 'loading':
return 'Loading';
case 'success':
return state.data;
case 'error':
return state.error.message;
default: {
const unreachable: never = state;
return unreachable;
}
}
}
```
## Untrusted data: `unknown`, then narrow
Values from `JSON.parse`, `fetch`, `localStorage`, environment variables and message events are `unknown` until checked. Never write `any` or cast them with `as User`.
A type guard must check every field the type promises, including the field types. Checking only `'id' in value` lets `{ id: 42 }` through as a `User`.
```ts
interface User {
id: string;
email: string;
role: 'admin' | 'user';
}
function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === 'object' && value !== null;
}
export function isUser(value: unknown): value is User {
return (
isRecord(value) &&
typeof value.id === 'string' &&
typeof value.email === 'string' &&
(value.role === 'admin' || value.role === 'user')
);
}
```
If the project already uses a schema library, define the schema once and infer the type from it (`type User = z.infer<typeof UserSchema>`) instead of writing guards by hand.
## Generics
- Add a type parameter only when it links two or more positions (an input to an output, or two inputs). A parameter used once should be a concrete type or `unknown`.
- Constrain type parameters to what the body uses.
```ts
export function findById<T extends { id: string }>(items: readonly T[], id: string): T | undefined {
return items.find((item) => item.id === id);
}
```
## Utility types and `satisfies`
- Derive related types instead of copying fields: `Partial<User>` for patches, `Pick<User, 'id' | 'email'>` for projections, `Omit<User, 'id'>` for create inputs, `Record<Role, Permission[]>` for complete lookup tables.
- Use `satisfies` to check a literal against a type while keeping its precise inferred type:
```ts
type Role = User['role'];
const permissions = {
admin: ['read', 'write', 'delete'],
user: ['read'],
} satisfies Record<Role, readonly string[]>;
```
A missing role, or a misspelled key, is a compile error.
## Verify before finishing
Run these when they are available in the project:
- `npx tsc --noEmit`
- `npx eslint .` (with `@typescript-eslint/no-explicit-any` and `no-unsafe-*` rules if configured)
- tests that feed malformed input to every new guard or schema
Cursor rules for strict TypeScript: compiler checks beyond strict, interfaces vs type aliases, real type guards, discriminated unions, utility types and branded IDs.
Stack-neutral CLAUDE.md conventions for Claude Code covering how to read before editing, keep diffs scoped, debug from a reproduction, test and report verification.
Copilot instructions for TypeScript project setup covering strict tsconfig flags and their effects, type-only imports, module hygiene and TSDoc for public APIs.
Copilot
general
Illustrative input and expected result. This example is a verification guide, not a recorded model run.
Content reviewed .
A TypeScript app parses JSON from an API and localStorage; these Copilot instructions are active while a developer asks for a typed parser and a role-permission table.
Parse the cached user from localStorage into a User { id: string; email: string; role: 'admin' | 'user' } and add a permissions table for each role. We don't use Zod.Illustrative; not a recorded model run. JSON.parse result kept as unknown and narrowed by an isUser guard that checks typeof id and email are strings and role is one of the two literals; no as User cast; a permissions object declared with satisfies Record<Role, readonly string[]> so a missing or misspelled role fails to compile; malformed-input test cases such as { id: 42 }.GitHub Copilot reads repository-wide custom instructions from .github/copilot-instructions.md and path-specific instructions from .github/instructions/*.instructions.md files with an applyTo glob. Copilot can also use AGENTS.md files, where the nearest file in the directory tree takes precedence. Which file types are honored depends on the surface: VS Code, Visual Studio, JetBrains, Xcode, Eclipse and GitHub.com differ.
.github/instructions/github-copilot-typescript-standards.instructions.mdStarting point, not a tested configuration for your repository. Used for files matching applyTo on surfaces that support path-specific instructions. Review the instructions, adapt the file paths and run your project's existing checks.
---
applyTo: "**/*"
---
# TypeScript Code Standards
Applies to: TypeScript source files (`**/*.ts`, `**/*.tsx`) when writing or reviewing functions, types and data handling.
Version baseline: checked against TypeScript 5.9 (the `satisfies` operator needs 4.9+). Read `package.json` for the installed version and for a validation library (Zod, Valibot, io-ts) before writing hand-rolled validation.
## Types for objects
- Use `interface` for object shapes that may be extended or implemented, and `type` for unions, intersections and mapped types. Follow the file's existing choice when both would work.
- Model closed sets of values as string-literal unions (`'admin' | 'user'`), not `string`.
- Model states with discriminated unions so that impossible combinations cannot be written.
```ts
type LoadState<T> =
| { status: 'idle' }
| { status: 'loading' }
| { status: 'success'; data: T }
| { status: 'error'; error: Error };
function label(state: LoadState<string>): string {
switch (state.status) {
case 'idle':
return 'Not started';
case 'loading':
return 'Loading';
case 'success':
return state.data;
case 'error':
return state.error.message;
default: {
const unreachable: never = state;
return unreachable;
}
}
}
```
## Untrusted data: `unknown`, then narrow
Values from `JSON.parse`, `fetch`, `localStorage`, environment variables and message events are `unknown` until checked. Never write `any` or cast them with `as User`.
A type guard must check every field the type promises, including the field types. Checking only `'id' in value` lets `{ id: 42 }` through as a `User`.
```ts
interface User {
id: string;
email: string;
role: 'admin' | 'user';
}
function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === 'object' && value !== null;
}
export function isUser(value: unknown): value is User {
return (
isRecord(value) &&
typeof value.id === 'string' &&
typeof value.email === 'string' &&
(value.role === 'admin' || value.role === 'user')
);
}
```
If the project already uses a schema library, define the schema once and infer the type from it (`type User = z.infer<typeof UserSchema>`) instead of writing guards by hand.
## Generics
- Add a type parameter only when it links two or more positions (an input to an output, or two inputs). A parameter used once should be a concrete type or `unknown`.
- Constrain type parameters to what the body uses.
```ts
export function findById<T extends { id: string }>(items: readonly T[], id: string): T | undefined {
return items.find((item) => item.id === id);
}
```
## Utility types and `satisfies`
- Derive related types instead of copying fields: `Partial<User>` for patches, `Pick<User, 'id' | 'email'>` for projections, `Omit<User, 'id'>` for create inputs, `Record<Role, Permission[]>` for complete lookup tables.
- Use `satisfies` to check a literal against a type while keeping its precise inferred type:
```ts
type Role = User['role'];
const permissions = {
admin: ['read', 'write', 'delete'],
user: ['read'],
} satisfies Record<Role, readonly string[]>;
```
A missing role, or a misspelled key, is a compile error.
## Verify before finishing
Run these when they are available in the project:
- `npx tsc --noEmit`
- `npx eslint .` (with `@typescript-eslint/no-explicit-any` and `no-unsafe-*` rules if configured)
- tests that feed malformed input to every new guard or schema
Ask for a small change to one matching file. Check the tool's loaded context or instruction view, inspect the diff for the requested convention, and run the relevant build or test. A well-formatted file does not guarantee the model follows every instruction.
Instruction support differs by surface: VS Code supports repository-wide, path-specific and AGENTS.md instructions; Visual Studio supports repository-wide and path-specific; JetBrains and Xcode use .github/copilot-instructions.md. GitHub's documentation describes AGENTS.md files stored anywhere in the repository, with the nearest file in the directory tree taking precedence, or alternatively a single CLAUDE.md or GEMINI.md in the root. Support differs by surface; GitHub's IDE page lists AGENTS.md among the instruction types for VS Code.
Documentation checked 2026-11-05: GitHub Docs: Adding repository custom instructionsGitHub Docs: Repository instructions in your IDE
Cursor rules for strict TypeScript: compiler checks beyond strict, interfaces vs type aliases, real type guards, discriminated unions, utility types and branded IDs.
Stack-neutral CLAUDE.md conventions for Claude Code covering how to read before editing, keep diffs scoped, debug from a reproduction, test and report verification.
Copilot instructions for TypeScript project setup covering strict tsconfig flags and their effects, type-only imports, module hygiene and TSDoc for public APIs.