Created
July 27, 2021 18:09
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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