Please enable JavaScript.
Coggle requires JavaScript to display documents.
Interfaces and Type Aliases - Coggle Diagram
Interfaces and Type Aliases
two mechanisms for centrally defining types and giving them useful and meaningful names
interfaces
type aliases
type aliases
{name: string, email: string}
passing this around as a return type can become burdensome
Type aliases help to address this
filename:
types.ts
UserContactInfo = {name: string, email:string}
import { UserContactInfo } from "./types"
Inheritance
combine existing types
using Intersection (&) types.
interface
is a way of defining an object type.
“an instance of a class could conceivably look like this”
Inheritance
implements
a given class should produce instances that confirm to a given interface
class Dog implements AnimalLike
Choosing which to use
If you need to define something other than an object type
you must use a type alias
If you need to define a type to use with the implements heritage term, it’s best to use an interface
If you need to allow consumers of your types to augment them, you must use an interface.