-
-
Notifications
You must be signed in to change notification settings - Fork 12.3k
feat(skills): add typescript-patterns skill (#543) #714
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| --- | ||
| name: typescript-patterns | ||
| description: TypeScript best practices, type safety, generics, async patterns, and project configuration for Claude Code and Codex. | ||
| --- | ||
|
Comment on lines
+1
to
+4
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This new skill is not wired into any install path yet. Manifest-driven installs only copy entries from Useful? React with 👍 / 👎. |
||
|
|
||
| # TypeScript Patterns | ||
|
|
||
| ## When to Use | ||
| - Working with TypeScript projects | ||
| - Adding type safety to JavaScript code | ||
| - Configuring tsconfig.json | ||
| - Using generics, utility types, or advanced type features | ||
|
|
||
| ## How It Works | ||
| 1. Apply strict TypeScript configuration | ||
| 2. Use proper type annotations and inference | ||
| 3. Leverage generics for reusable components | ||
| 4. Handle async/await with proper error types | ||
|
|
||
| ## Key Patterns | ||
|
|
||
| ### Strict Configuration | ||
| Always enable strict mode in tsconfig.json: strict, noUncheckedIndexedAccess, exactOptionalPropertyTypes. | ||
|
|
||
| ### Type-Safe Error Handling | ||
| Use discriminated unions for error types instead of throwing. Return Result types for fallible operations. | ||
|
|
||
| ### Generic Constraints | ||
| Constrain generics with extends to ensure type safety while maintaining flexibility. | ||
|
|
||
| ### Async Patterns | ||
| Use async/await with typed catch blocks. Prefer Promise.allSettled over Promise.all for parallel operations that can partially fail. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🌐 Web query:
💡 Result: No, in modern TypeScript, catch clause variables cannot be explicitly type-annotated with types other than 'any' or 'unknown'. Attempting to use any other specific type (like Error or a custom type) results in the compiler error TS1196: "Catch clause variable type annotation must be 'any' or 'unknown' if specified." This restriction exists because JavaScript can throw values of any type (not just Error objects), and TypeScript cannot statically verify what will be thrown. TypeScript 4.0 introduced support for annotating catch variables as 'unknown' (safer than 'any'), and TypeScript 4.4 added the tsconfig option 'useUnknownInCatchVariables' (part of strict mode) to make unannotated catch variables default to 'unknown' instead of 'any'. The recommended pattern for catch error handling is: 1. Use 'catch (error: unknown)' or enable 'useUnknownInCatchVariables' so unannotated variables are 'unknown'. 2. Use type guards (instanceof Error, property checks, or custom type predicates) to narrow the type before accessing properties. 3. Avoid type assertions unless necessary, as they bypass safety. Example: try { riskyOperation; } catch (error: unknown) { if (error instanceof Error) { console.error(error.message); // Safe: error is Error } else { console.error('Unknown error:', error); } } Utility helper pattern (common best practice): function getErrorMessage(error: unknown): string { if (error instanceof Error) return error.message; return String(error); } This ensures type safety while handling any thrown value. For domain-specific errors, throw custom Error subclasses and check with instanceof. Citations:
Clarify catch block error handling guidance for TypeScript accuracy. Line 32 states "Use async/await with typed catch blocks," but this wording is ambiguous and could suggest explicit catch-parameter typing is a standard pattern. Rephrase to "use 🤖 Prompt for AI Agents |
||
|
|
||
| ### Utility Types | ||
| Use Pick, Omit, Partial, Required, Record for type transformations instead of manual type definitions. | ||
|
|
||
| ## Examples | ||
| - Use zod for runtime validation that generates TypeScript types | ||
| - Prefer const assertions for literal types | ||
| - Use satisfies operator for type checking without widening | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because
npm testrunsnode scripts/ci/catalog.js --text(package.json:107), adding this 116th skill without updating the documented totals makes the default validation fail for every branch that includes this commit. Running the catalog check on top of this change reportsskills: 116whileREADME.mdandAGENTS.mdstill advertise 115, so CI will stay red until those counts are bumped.Useful? React with 👍 / 👎.