Suggest an editImprove this articleRefine the answer for “The type does not satisfy the constraint”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)If a **generic parameter does not satisfy its constraint (**`extends`**)**, TypeScript **produces a compile-time error**, meaning the code **will not compile** until a matching type is substituted. **Key point:** TypeScript checks whether type `T` is assignable to type `U`, and it does not try to automatically convert types - it simply disallows the mismatch.Shown above the full answer for quick recall.Answer (EN)ImageIf a **generic parameter does not satisfy its constraint (**`extends`**)**, TypeScript **produces a compile-time error** - that is, the code **will not compile** until you substitute a matching type. --- ## What "does not satisfy the constraint" means When you write, for example: ```javascript function fn<T extends { id: number }>(value: T) { ... } ``` you are telling TypeScript: > "Only allow types `T` that **must have a field** `id: number`." If you try to pass a type without `id`, the compiler will say: *"Type X does not satisfy the constraint Y"*. --- ## Example 1: An error on a constraint mismatch ```javascript function printId<T extends { id: number }>(obj: T) { console.log(obj.id); } printId({ id: 42 }); // fits printId({ name: "Tim" }); // Error: // Argument of type '{ name: string }' is not assignable to parameter of type '{ id: number; }'. ``` > `T` must be a subtype of `{ id: number }`, but `{ name: string }` does not fit. --- ## Example 2: A mismatch with a union ```javascript function toStringValue<T extends string | number>(value: T): string { return value.toString(); } toStringValue("hello"); // ok toStringValue(100); // ok toStringValue(true); // Error: 'boolean' does not satisfy the constraint 'string | number'. ``` > The type `boolean` is not part of the allowed set (`string | number`). --- ## Example 3: A mismatch with a dependent parameter ```javascript function getProp<T, K extends keyof T>(obj: T, key: K) { return obj[key]; } const user = { id: 1, name: "Tim" }; getProp(user, "id"); // ok getProp(user, "age"); // Error: Type '"age"' is not assignable to parameter of type '"id" | "name"' ``` > `K` must be a **key of the object** `T`, but `"age"` is not one. --- ## Example 4: A mismatch with the `object` constraint ```javascript function logKeys<T extends object>(obj: T) { console.log(Object.keys(obj)); } logKeys({ a: 1 }); // ok logKeys(42); // Error: number does not satisfy the constraint 'object' ``` > Primitives (`number`, `string`, `boolean`) are not objects. --- ## Example 5: A mismatch for classes (constructor constraints) ```javascript type Constructor<T> = new (...args: any[]) => T; function createInstance<T extends Constructor<any>>(Ctor: T) { return new Ctor(); } class Person {} createInstance(Person); // ok createInstance(123); // Error: number does not satisfy the constraint 'new (...args: any[]) => any' ``` > `123` is not a constructor, so it does not fit the constraint. --- ## Example 6: An error from violating a template constraint ```javascript type Prefixed<T extends `id_${string}`> = { key: T }; const good: Prefixed<"id_123"> = { key: "id_123" }; // ok const bad: Prefixed<"user_1"> = { key: "user_1" }; // Type '"user_1"' does not satisfy the constraint '`id_${string}`' ``` > `T` must match the template string `id_...`. --- ## What happens under the hood When TypeScript sees `T extends U`, it checks: > Can the type `T` be **assigned** to the type `U` (is `T` assignable to `U`). If not, a compile-time error: ```javascript Type 'T' does not satisfy the constraint 'U'. ``` TypeScript **does not try to automatically convert** types - it simply disallows the mismatch. --- ## An example with an explicit type ```javascript function identity<T extends number>(value: T) { return value; } identity(42); // ok identity<number>(42); // ok identity<string>("hi"); // Type 'string' does not satisfy the constraint 'number' ``` > Even if you specify the generic explicitly, the compiler checks whether it fits the `extends` constraint. --- ## Why this is useful 1. **Type safety:** you cannot accidentally pass an unsuitable type. 2. **Autocomplete:** inside the function, TS knows which properties are guaranteed. 3. **Flexibility:** you can set "soft" constraints (for example, `{ id: any }`). --- ## Summary | Situation | Example | What happens | |---|---|---| | The type satisfies the constraint | `<T extends { id: number }>` + `{ id: 1 }` | It works | | The type is missing the needed properties | `{ name: string }` | Error | | The type is not part of the union | `<T extends string | number>`+`boolean` | | The key does not exist | `<K extends keyof T>` + `"age"` | Error | | The type is not an object | `<T extends object>` + `number` | Error |For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.