CodexBloom - Programming Q&A Platform

TypeScript: guide with Extending Interfaces in a Library with Mapped Types

👀 Views: 9633 đŸ’Ŧ Answers: 1 📅 Created: 2025-06-12
typescript interfaces mapped-types TypeScript

I'm sure I'm missing something obvious here, but Does anyone know how to I'm working on a project and hit a roadblock. I'm working with a scenario with extending an interface in TypeScript while using mapped types in a library I'm building. I've defined a basic interface and I'm trying to extend it to create a new type with additional properties, but TypeScript is throwing an behavior that I need to seem to figure out. Here's the initial interface I have defined: ```typescript interface BaseConfig { name: string; version: number; } ``` Now, I want to create a new configuration type that extends `BaseConfig` and adds an optional `description` property. I thought I could do this using mapped types, like so: ```typescript type ExtendedConfig<T> = T extends BaseConfig ? T & { description?: string } : never; ``` However, when I try to use `ExtendedConfig` like this: ```typescript const config: ExtendedConfig<BaseConfig> = { name: 'MyApp', version: 1.0, description: 'An awesome app!' }; ``` I get the following behavior: ``` Type '{ name: string; version: number; description?: string; }' is not assignable to type 'ExtendedConfig<BaseConfig>'. Type '{ name: string; version: number; description?: string; }' is not assignable to type 'never'. ``` I've also tried defining `ExtendedConfig` without the conditional type, just like this: ```typescript type ExtendedConfig = BaseConfig & { description?: string }; ``` But then I lose the flexibility of using the mapped type with other configurations. I've checked that my TypeScript version is 4.5.2, and I need to find anything about this specific scenario in the documentation. Any insights on how to correctly extend the `BaseConfig` interface while using mapped types would be greatly appreciated! My team is using Typescript for this microservice. I'd be grateful for any help. I'm working on a application that needs to handle this.