YouTip LogoYouTip

Ts Interface

TypeScript Interfaces \n\n

An interface is a declaration of a series of abstract methods, a collection of method characteristics. These methods are all abstract and need to be implemented by concrete classes. Then, third parties can call this set of abstract methods to make the concrete classes execute the specific methods.

\n\n

The TypeScript interface is defined as follows:

\n\n
interface interface_name { }
\n\n

Example

\n\n

In the following example, we define an interface IPerson, and then define a variable customer whose type is IPerson.

\n\n

The customer implements the properties and methods of the interface IPerson.

\n\n

TypeScript

\n\n
interface IPerson{\n    firstName:string,\n    lastName:string,\n    sayHi: ()=>string\n}\n\nvar customer:IPerson = {\n    firstName:"Tom",\n    lastName:"Hanks",\n    sayHi: ():string =>{\n        return "Hi there"\n    }\n}\n\nconsole.log("Customer object ")\nconsole.log(customer.firstName)\nconsole.log(customer.lastName)\nconsole.log(customer.sayHi())\n\nvar employee:IPerson = {\n    firstName:"Jim",\n    lastName:"Blakes",\n    sayHi: ():string =>{\n        return "Hello!!!"\n    }\n}\n\nconsole.log("Employee object ")\nconsole.log(employee.firstName)\nconsole.log(employee.lastName)
\n\n

Note that interfaces cannot be converted to JavaScript. They are only part of TypeScript.

\n\n

Compiling the above code yields the following JavaScript code:

\n\n

JavaScript

\n\n
var customer = {\n    firstName: "Tom",\n    lastName: "Hanks",\n    sayHi: function(){\n        return "Hi there";\n    }\n};\n\nconsole.log("Customer object ");\nconsole.log(customer.firstName);\nconsole.log(customer.lastName);\nconsole.log(customer.sayHi());\n\nvar employee = {\n    firstName: "Jim",\n    lastName: "Blakes",\n    sayHi: function(){\n        return "Hello!!!";\n    }\n};\n\nconsole.log("Employee object ");\nconsole.log(employee.firstName);\nconsole.log(employee.lastName);
\n\n

The output is:

\n\n
Customer object\nTom\nHanks\nHi there\nEmployee object\nJim\nBlakes
\n\n
\n\n

Union Types and Interfaces

\n\n

The following example demonstrates how to use union types within an interface:

\n\n

TypeScript

\n\n
interface RunOptions{\n    program:string;\n    commandline:string[]|string|(()=>string);\n}\n\nvar options:RunOptions = {\n    program:"test1",\n    commandline:"Hello"\n};\n\nconsole.log(options.commandline)\n\noptions = {\n    program:"test1",\n    commandline:["Hello","World"]\n};\n\nconsole.log(options.commandline);\nconsole.log(options.commandline);\n\noptions = {\n    program:"test1",\n    commandline:()=>{return "**Hello World** "}\n};\n\nvar fn:any = options.commandline;\nconsole.log(fn());
\n\n

Compiling the above code yields the following JavaScript code:

\n\n

JavaScript

\n\n
var options = {\n    program: "test1",\n    commandline: "Hello"\n};\n\nconsole.log(options.commandline);\n\noptions = {\n    program: "test1",\n    commandline: ["Hello", "World"]\n};\n\nconsole.log(options.commandline);\nconsole.log(options.commandline);\n\noptions = {\n    program: "test1",\n    commandline: function(){return "**Hello World** "; }\n};\n\nvar fn = options.commandline;\nconsole.log(fn());
\n\n

The output is:

\n\n
Hello\nHello\nWorld\n**Hello World**
\n\n
\n\n

Interfaces and Arrays

\n\n

In interfaces, we can set the index value and elements of an array to different types. The index value can be a number or a string.

\n\n

Setting the element type to string:

\n\n

Example

\n\n
interface namelist{\n    [index:number]:string\n}\n\nvar list2:namelist = ["Google","Tutorial","Taobao"]
\n\n

If another type is used, an error will occur:

\n\n

Example

\n\n
interface namelist{\n    [index:number]:string\n}\n\nvar list2:namelist = ["John",1,"Bran"]
\n\n

After execution, an error is reported as follows, indicating a type mismatch:

\n\n
test.ts:8:30 - error TS2322: Type 'number' is not assignable to type 'string'.\n\n8 var list2:namelist = ["John",1,"Bran"]\n                     ~\ntest.ts:2:4\n  2 [index:number]:string\n    ~~~~~~~~~~~~~~~~~~~~~\n    The expected type comes from this index signature.\n\nFound 1 error.
\n\n

TypeScript

\n\n
interface ages{\n    [index:string]:number\n}\n\nvar agelist:ages;\nagelist = 15
\n\n
\n\n

Interface Inheritance

\n\n

Interface inheritance means that an interface can extend itself through other interfaces.

\n\n

TypeScript allows an interface to inherit from multiple interfaces.

\n\n

Inheritance uses the keyword extends.

\n\n

Single interface inheritance syntax:

\n\n
Child_interface_name extends super_interface_name
\n\n

Multiple interface inheritance syntax:

\n\n
Child_interface_name extends super_interface1_name, super_interface2_name,…,super_interfaceN_name
\n\n

The inherited interfaces are separated by commas ,.

\n\n

Single Inheritance Example

\n\n

TypeScript

\n\n
interface Person{\n    age:number\n}\n\ninterface Musician extends Person{\n    instrument:string\n}\n\nvar drummer = <Musician>{};\ndrummer.age = 27\ndrummer.instrument = "Drums"\n\nconsole.log("Age: "+drummer.age)\nconsole.log("favorite musical instrument: "+drummer.instrument)
\n\n

Compiling the above code yields the following JavaScript code:

\n\n

JavaScript

\n\n
var drummer = {};\ndrummer.age = 27;\ndrummer.instrument = "Drums";\n\nconsole.log("Age: " + drummer.age);\nconsole.log("favorite musical instrument: " + drummer.instrument);
\n\n

The output is:

\n\n
Age: 27\nfavorite musical instrument: Drums
\n\n

Multiple Inheritance Example

\n\n

TypeScript

\n\n
interface IParent1{\n    v1:number\n}\n\ninterface IParent2{\n    v2:number\n}\n\ninterface Child extends IParent1, IParent2{}\n\nvar Iobj:Child = {v1:12, v2:23}\n\nconsole.log("value 1: "+Iobj.v1+" value 2: "+Iobj.v2)
\n\n

Compiling the above code yields the following JavaScript code:

\n\n

JavaScript

\n\n
var Iobj = {v1: 12, v2: 23};\n\nconsole.log("value 1: " + Iobj.v1 + " value 2: " + Iobj.v2);
\n\n

The output is:

\n\n
value 1: 12\nvalue 2: 23
← Ts ObjectTs Tuple β†’