Skip to content

Instantly share code, notes, and snippets.

@iAmWillShepherd
Created July 27, 2021 18:09
Show Gist options
  • Save iAmWillShepherd/a8a34464c99579fc7a6d7bd3fa4e0005 to your computer and use it in GitHub Desktop.
Save iAmWillShepherd/a8a34464c99579fc7a6d7bd3fa4e0005 to your computer and use it in GitHub Desktop.
Use an intersection type when need to extend a type and the extends keyword when extending interfaces.
// Create intersection type to "extend" types by combining them
type BaseType = {
readonly id: string
}
const intersection: BaseType & { kind: string } = {
id: ‘123’,
kind: ‘intersection’
}
// Extend an interface to add additional properties
interface BaseInterface {
readonly id: string,
}
interface extended extends BaseInterface {
kind: string
}
const extended: extended = {
id: ‘123’,
kind: ‘intersection’
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment