What is a generic — and why any in AI-written code kills your autocomplete

Here's a familiar scene. You ask an AI: "write a function that returns the first item of a list." It writes it in TypeScript, and it works. You call first(users), type a dot — and the editor goes quiet. No name, no email in the suggestions.
The code isn't broken. The AI just wrote the type any, and TypeScript stopped knowing what comes back. The fix is one letter in angle brackets. That letter is a generic.
What a generic is — in one line
A generic is a variable for a type. A regular variable holds a value. A generic holds a type: "whatever I'm handed goes here."
Here's the function with any:
function first(items: any[]): any {
return items[0];
}
And here it is with a generic:
function first<T>(items: T[]): T {
return items[0];
}
The difference is <T>. Think of a label on a box: "whatever went in is what's inside." Pass an array of users — T becomes "user." Pass an array of numbers — T becomes number.
How it works
The nice part: you almost never write T by hand when calling. TypeScript looks at what you passed and fills in the type itself.
const user = first(users); // T = User
const n = first([3, 7, 9]); // T = number
Now after user. the editor suggests name and email again. And if you write n.toUpperCase(), it underlines the mistake before you even run anything. Numbers don't have that method.
With any, that bug would slip through and show up in the browser. any means "check nothing." A generic means "check, but adapt to what you were given."
The letter T is just tradition, short for type. You could call it Item. Sometimes there are several: <K, V> — "key and value."
You already use generics
You've probably met them without knowing the name:
Array<string>— same asstring[]. An array, with the element type passed as a parameter.Promise<User>— a promise that a user will arrive at some point. Everything you do with async/await is wrapped in a type like this.useState<User | null>(null)in React. Without the<...>, React would decide the state can only ever holdnull.Map<string, number>— a dictionary with string keys and number values.
Same idea everywhere: one container, different contents. And the container remembers what's inside.
A generic in your own code
The most common place a beginner needs their own generic is a server response. Every request has the same wrapper, but different contents:
type ApiResponse<T> = {
data: T;
error: string | null;
};
const res: ApiResponse<User[]> = await getUsers();
One definition for every request in the app. For the user list, T is an array of users; for an order, it's an order. And after res.data[0]. the editor knows every field again.
Without a generic you'd write a separate type for each request. Or give up and use any — and lose autocomplete again.
Why it matters to you
You probably won't write fancy generics yourself. But you will read AI-written code. There, a generic is a good sign, and any is a reason to look closer.
How to use this in practice:
- See
anyon a function that just passes data along? Ask for a generic instead. - Autocomplete vanished after the dot? First look for an
anysomewhere in the call chain. - Asking the AI for a utility? Say up front you want types without
any.
Make a function that takes the first item of an arrayWrite a TypeScript function first that returns the first item of an array. No any: use a generic so the result type matches the element type. For an empty array return undefined and reflect that in the type. Show two calls: with an array of numbers and with an array of User objects.Notice the empty array. The honest version is T | undefined, because an empty list has no first item. A good type doesn't lie about cases like that. More on how TypeScript guards you here — in TypeScript vs JavaScript.
Are a generic and any the same thing?
No. any turns checking off entirely: you can call anything and get no error. A generic keeps checking on; the type is just figured out at the moment of the call.
Why does AI write any so often?
It's the fastest way to get rid of the red squiggle, and the AI tries to hand you code that compiles. Ask directly: "no any." Many teams also turn on a linter rule that bans any.
Does plain JavaScript have generics?
No, it's a TypeScript layer. JavaScript has no types while you write code, so there are no variables for types either. After the build, every <T> is simply erased.
Can I limit what fits into T?
Yes, with the word extends. For example, <T extends { id: string }> means "any object, but it must have an id field." Handy when a function looks a record up by id.
The AI Architect 2027 course: step by step from computer basics to your own app and first customers. 543 short lessons on your phone.





