typescript interface vs type

Solutions on MaxInterview for typescript interface vs type by the best coders in the world

showing results for - "typescript interface vs type"
Alessandro
21 Apr 2017
1/**
2* Modern Typescript types and interfaces are both very powerful (2020)
3* It is almost down to personal choice, these days. 
4* Key differences:
5   - The type alias can also be used for other types such as primitives, 
6   unions, and tuples.
7   - Unlike a type alias, an interface can be defined multiple times, 
8   and will be treated as a single interface
9* Decision on when to use:
10   - Personally, I use the interface to make it clear that it 
11   is intended to be implemented and types for everything else.
12*/
13
14// type
15type Person = {
16  name: string;
17  age: number;
18}
19
20type speak = (sentence: string) => void;
21
22// interface
23interface Person extends Human {
24  name: string;
25  age: number;
26}
27
28interface speak {
29  (sentence: string): void;
30}
Chiara
31 Nov 2019
1// Types are used to enforce the data type of a variable
2// For example:
3
4type Weekday = 'Monday' | 'Tuesday' | 'Wednesday' | 'Thursday' | 'Friday' | 'Saturday' | 'Sunday';
5
6let day1: Weekday = 'Monday';  // OK
7let day2: Weekday = 'January'; // ERROR: 'January' is not assignable to type 'Weekday'
8
9// Interfaces are used to enforce the shape of an object
10// For example:
11
12interface Person {
13  firstName: string;
14  lastName: string;
15  age: number;
16}
17
18// OK
19let person1: Person = {
20    firstName: 'James',
21    lastName: 'Smith',
22    age: 30
23}
24
25// ERROR: property 'age' is missing
26let person2: Person = {
27    firstName: 'Mary',
28    lastName: 'Williams'
29}
30
31// NOTE: Technically, you can use types to enforce the shape of objects,
32//       but that's not what they are intended for.
33
34// More details: https://www.typescriptlang.org/docs/handbook/advanced-types.html
Caterina
21 Mar 2016
1// for starters:
2// interfaces and types are very similar, they just differ in syntax
3
4// interface
5interface Car {
6  name: string;
7  brand: string;
8  price: number;
9}
10
11// type
12type Car = {
13  name: string;
14  brand: string;
15  price: number;
16}
17
18// interface
19interface Car extends Vehicle {
20  name: string;
21  brand: string;
22  price: number;
23}
24
25// type
26type Car = Vehicle & {
27  name: string;
28  brand: string;
29  price: number;
30}
queries leading to this page
type vs interface typescript 22 26 22difference between interface and types typescripttypescript when to use interface vs classwhat is difference between interface and type in typescriptdifference between interface and type typecriptinterface vs type tstypescript interface vs classtypes vs interfaces typescriptclass object vs interface typescriptinterfaces vs types typescripttypescript type or interfaceinterface vs type in tstypes vas interface tswhen to use type vs interface typescriptinterface or type typescripttypes vs interface typescrtiptjavascript interface vs typescirpttypescript class and interface differencetypescript object type vs interfacetypescript type vs interface exampletypescript declare vs interfacetypescript when to use type vs interfacewhat hs difference between type and interface in typescriptclasses vs interfaces in typescripttypescript type interface differencedifference between interface and class in typescriptwhen to use typescript typestypescript difference type interfacetype vs interface vs class typescripttypescript interface e typeclass vs interface tstypescript class vs interface vs typedifference interface class typescriptinterface data types in typescriptclasses vs interfaces typescripttypescript interface different typestype vs interface jstype interface difference typescriptjavascript difference between type and interfacetype vs interface typescripttypescript when to use class vs interfaceclass vs interface vs type typescriptts types vs interfacesdifference between class and interface typescriptinterface and type typescriptdifference between type and interface in typescriptdeclare vs interface typescripttypescript interface vstypescript angular interface vs classwhen should i use a type vs interface typescripttypescript difference between interface and typeinterface vs type objectdifference type interface typescripttypes 26 vs 7c 3ftype versus interfaceinterface vs class when to use typescripttypescript interface vs type literaltypescript difference between type and interfaceusing interface vs type in typescriptwhen to use interface vs type typescripttypescript interface vs objectinterface vs typeof typescriptinterface vs model typescriptinterface vs typesinterface vs class typescript angulartypescript class vs interfaceis type and interface the sameinterface and class difference in typescriptinterface vs type vs class typescripttypescript type vs interface reactinterface vs class in typescripttype vs interface in tstypescript difference type and interfaceinterface vs class typescripttypescript difference between a type and a interfaceannotation vs interface typescripttypescript type vs interfacedifference between interface and type typescriptangular type vs interfacets type vs interfacejs interface vs typewhat is the difference between interface and type in typescripttypescript interface type nameinterface vs type in typescripttypescript export type vs classtypescript type of interface propertyinterfaces vs typescriptreact typescript type vs interfaceinterface vs types in tstypescript type vs interface 2020interface vs type typescript for objecttypescript interface vs typestypescript interface vs modeltypescript interface versus typetype vs interface in typescripttype in tsx vs interfacetypescript types vs interfacestype vs interfacedifferences type vs interface typescripttype vs interface in typscriptclass vs interface typescript for optionsclass vs interface typescriptwhat is the difference between interface and class typescripttypes vs interfacetypescript initialize interface vs classtypes vs interfaces in typescriptwhich is better type or interface typescriptwhen to use interface vs typetypes vs interfaceswhen to user interface vs typestypescript type and interfacetypescript model interface vs classclass vs interface typescript features typescript difference between types and interfacesdifference between interface and type in typscriptwhat is the difference between interface and annotations and type in typescripttypescript type and interface differenceexport type vs export interfaceinterface vs types in typescriptdifference between type and interface in typesscripttypescript type of interface and classwhy would you use an interface in typtypescript difference between type and interfaceinterface vs typetypescript use interface and type keywordtype and interface typescripttypescript interface vs class for modeltypescript when to use interface vs typetypescript declare type vs interfaceinterface vs type typescript reactdifference type and interface typescripttypes and interfacestype versus interface typescriptinterfaces in typescripttypescript interfaces vs classests interface vs typedifference between type and interface in tsdifference between types and interface typescripttypescript interface type differencetypescript two interfaces same namewhat is the difference between 22interface vs type 22 statements in typescriptinterface as type alias typescriptts type vs interface examples variableusing an extednded interface as a typedeftypescript what the difference between type and interfacetypescript use interface as typetypescript interfaces vs typestypescript interface or typewhat is the difference between type and interface in typescripttypescript custom type vs interfacedifference between defining an interface and keyword type in typescriptinterface vs type typescript diferenciasinterface vs class typescriptdifference between type and interface tstype vs interface reacttypescript when to use interface and typewhat is difference between type and interface in typescripttype extends typescript vs interfacetype vs interface typesscirptinterface vs types typescriptdifference between type and interface typescriptwhich interface type is the bestinterface vs type typescripttypescript interface vs type vs classdifference between interface and type in typescriptdifference type vs interface typescriptinterface and type difference tsdifferences between interface and type typscripttypescript interface vs typetypescript define type vs interfacets class vs interfacereact typescript interface vs typetypescript when use interface vs classtypescript difference between custom type and interfacetypescript classes vs interfacetype vs interface typescript reactjava interface vs typescript interfaceclass vs interface in typescriptdifference between interface and type tsdifferent of type and interface typescripttypescript interface vs type