Langsung ke konten
Skillpedia
Pemrograman umum15k · #11002

typescript-best-practices

Fungsinya

Skill ini memberi aturan untuk menulis kode TypeScript yang andal. Untuk developer yang ingin lebih sedikit error tipe dan lebih sedikit crash tersembunyi.

Kapan dipakai

  • ✓Saat Anda meminta agent menulis atau mengedit file TypeScript atau TSX.
  • ✓Saat Anda ingin cast tipe yang tidak aman atau tipe yang longgar dihapus dari kode Anda.
  • ✓Saat Anda ingin data dari luar diperiksa sebelum kode mengandalkannya.

Link skill

https://github.com/cursor/plugins/blob/main/harness/pstack/claude-code/skills/typescript-best-practices/SKILL.md

Salin lalu tempelkan ke AI Anda (Claude, ChatGPT, atau Cursor) dan minta AI mengikuti instruksinya.

Cara pakai

Cara paling mudah: salin pesan ini dan tempelkan ke AI Anda. AI akan membaca skill dan mengikuti instruksinya.

Pesan siap tempel

Gunakan skill ini untuk membantu saya: https://github.com/cursor/plugins/blob/main/harness/pstack/claude-code/skills/typescript-best-practices/SKILL.md Baca file SKILL.md di link tersebut dan ikuti instruksinya untuk tugas saya.
Mau AI Anda menemukan skill seperti ini sendiri?Pasang MCP Skillpedia gratis: cukup satu baris, dan AI mencari di antara 15.000+ skill sebelum tiap tugas.Pasang dalam 1 menit →

Contoh permintaan ke AI

“Rapikan tipe di file TypeScript ini agar tidak bergantung pada cast yang tidak aman.”

5 skill baru setiap minggu. 1 menit baca.

Cara kerjanya

Agent mengikuti sejumlah aturan, seperti memakai varian berlabel, memakai unknown untuk data dari luar, dan memeriksa data dengan skema sebelum mempercayai tipenya. Aturan ini mencegah keadaan yang mustahil masuk ke dalam kode.

Konten teknis (SKILL.md asli)

Teks asli dari pembuat, dalam bahasa Inggris, langsung dari GitHub.

TypeScript best practices

Apply the type-system-discipline principle skill first.

Rule Summary
Discriminated unions Model variants with a kind literal discriminant so impossible states can't be represented. No optional-field bags.
Branded types Brand primitives with & { readonly __brand: "X" } so they can't be mixed up. Validate once at the boundary. A brand comes from a verifying type guard (input is AgentId), never from as.
Constructive modeling Build the shape so the illegal value can't be constructed. [T, ...T[]] for non-empty, [T, T][] for even length, start plus duration for a range. Not a runtime guard, not a wish for refinement types.
Simplest total type Keep T[] while every operation on it stays total. Strengthen to NonEmpty<T> only where the loose type forces !, a cast, or a "should never happen" throw.
unknown over any External data is unknown.
Schemas before guards Before hand-writing a property-by-property type guard, use the repository's runtime schema library and infer the type from the schema, such as z.infer.
No as casts Every as is a runtime crash waiting. Cast only after validation.
Narrowing hierarchy Discriminant switch > in operator > typeof/instanceof > user-defined type guard > as.
Type guards Must verify the claim. A lying guard is worse than as because the bug hides behind a name that says it's safe. Name them isX or hasX.
Exhaustiveness Inline const _exhaustive: never = x; in default arms so the compiler errors when a new variant is added.
satisfies over as Validates the value without widening literal types.
Boundary validation Parse where data crosses in, into a named domain type. Record<string, unknown> (however spelled) stops at that parse. Trust types inside. See the boundary-discipline principle skill.
Schema-derived types Reach for Pick/Omit/Parameters/ReturnType/Awaited/typeof before declaring a new interface.
Object args Pass objects, not positional, so argument order is self-documenting. Skip on hot paths (per-frame render, tokenizers, parsers).
Real tests Don't mock what you can run. Prefer the framework's real test primitives with leak/disposable checks, and verify UI in a running build. Mock only what you can't run locally.
Structured telemetry Prefer structured logger diagnostics with enough context to debug from an id. No console.log in shipped code.

Examples: references/patterns.md.

Pertanyaan umum

Apakah berlaku untuk file JavaScript biasa?

Aturannya untuk file TypeScript, yaitu .ts dan .tsx.

Apa itu branded type?

Tipe yang menandai sebuah nilai, misalnya ID, agar tidak tertukar dengan nilai lain.

Mengapa menghindari cast dengan as?

Cast memberi tahu compiler untuk mempercayai nilai tanpa memeriksanya, sehingga kode bisa gagal saat dijalankan.

Detail

Deskripsi asli dari pembuat

TypeScript best practices. Use when reading or editing any .ts or .tsx file.

repo: cursor/plugins

Popularitas

Bintang GitHub
15k
Peringkat
#11002
Lisensi
—
Diperbarui
31/10/2026

Apakah skill ini berguna?

0

Masuk untuk memberi suara.

Komentar

  • …

Skill lain dari repositori ini

Skill serupa