OTFotf
All posts
General

Getting AI to Write TSL That Works

9 min
Getting AI to Write TSL That Works

AI-generated TypeScript that actually compiles and passes type checking. Practical prompt patterns and context strategies for correct type-safe code.

The TypeScript Challenge

TypeScript is simultaneously the best and worst language for AI code generation. Best because the type system provides guardrails — the compiler catches many AI mistakes. Worst because AI tools frequently generate code that looks correct but fails type checking.

The root cause: AI tools often generate code based on JavaScript patterns and retrofit TypeScript types as an afterthought. This produces any type escapes, incorrect generic parameters, and type assertions that hide real errors.

The solution: provide enough type context for the AI to generate genuinely type-safe code from the start, not patched-on types after the fact.

Providing Type Context

The most important factor in AI-generated TypeScript quality is the type context you provide.

1

Include relevant type definitions

Before asking for implementation, paste the type definitions the code will work with. If you're building a function that processes User objects, include the User type. Claude Code and Cursor both produce dramatically better TS when they can see the exact types.

2

Specify return types explicitly

Don't just say 'write a function that processes users.' Say 'write a function that takes User[] and returns ProcessedUser[]. Here are both types.' Explicit input and output types constrain the AI to correct code.

3

Show existing patterns

Paste an existing similar function from your codebase. 'Follow this pattern for the new function' gives the AI your generic patterns, error handling types, and utility type usage.

4

Include tsconfig strictness

Mention your strictness level: 'We use strict: true with noUncheckedIndexedAccess.' This prevents the AI from relying on permissive compiler settings that your project doesn't allow.

Prompt Patterns for TypeScript

Type-First Prompting

Write the types BEFORE asking for implementation. 'Here are the types: [paste types]. Now implement the function that satisfies this signature: [paste function signature].' The AI generates implementation to match types, not types to match implementation.

No-Any Rule

Add this to every TypeScript prompt: 'Do not use `any` type anywhere. Use `unknown` with type guards when the type is genuinely uncertain.' This single instruction eliminates the most common AI TypeScript mistake.

Generic Constraint Patterns

When working with generics, provide explicit constraint examples: 'Use this generic pattern: function process<T extends BaseEntity>(items: T[]): T[].' AI tools handle constrained generics much better than unconstrained ones.

Discriminated Union Patterns

For complex state types, specify: 'Use discriminated unions with a kind/type field, not optional properties.' This produces type-safe state machines instead of nullable field soup.

Common AI TypeScript Pitfalls

Type Assertions (as) Abuse

AI tools love `as SomeType` to silence errors. This hides real bugs. Add to your prompt: 'Never use type assertions (as). If a type doesn't match, fix the implementation, not the type.' Only exception: `as const` is fine.

Incorrect Generic Inference

AI often generates generics that don't infer correctly at call sites. Always test the generated code at the call site, not just the definition. If TypeScript can't infer the generic parameter, the generic is wrong.

Ignoring Null/Undefined

AI code frequently accesses potentially null values without checking. With strictNullChecks enabled, this fails compilation. Remind the AI: 'Handle null and undefined cases explicitly. No non-null assertions (!).'

Enum Misuse

AI tools generate TypeScript enums by default. For most cases, const objects with `as const` are better: more type-safe, tree-shakeable, and no runtime overhead. Specify your preference in the prompt.

More resources

On this page