Skip to content

Instantly share code, notes, and snippets.

@natterstefan
Created September 5, 2020 18:27
Show Gist options
  • Save natterstefan/b6facddaf06cdfddfe9b8ad9627f0548 to your computer and use it in GitHub Desktop.
Save natterstefan/b6facddaf06cdfddfe9b8ad9627f0548 to your computer and use it in GitHub Desktop.
TypeScript | Declaration Merging
/**
* Why is any so important?
*
* > Non-function members of the interfaces should be unique. If they are not unique,
* they must be of the same type. The compiler will issue an error if the interfaces
* both declare a non-function member of the same name, but of different types.
* @see https://www.typescriptlang.org/docs/handbook/declaration-merging.html#merging-interfaces
*
* Docs
* @see https://www.typescriptlang.org/docs/handbook/declaration-merging.html
*/
// in the library
interface BaseImageSettings {
// ATTENTION: must be any; or the app will not be able to properly augment this type
imageVariation: any
}
interface IImageSettings extends BaseImageSettings {}
// in the app
interface IImageSettings {
imageVariation: 'default' | 'custom'
}
const test: IImageSettings = {
imageVariation: 'default' // ✅
}
const test2: IImageSettings = {
imageVariation: 'custom' // ✅
}
const test3: IImageSettings = {
imageVariation: 'fails' // 🚨
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment