Interface in TypeScript
In this post we will see how to declare Interface in TypeScript and the using this interface for passing objects to function.
In below code we have interface Point which have defining of holding two variables ‘x’ and ‘y’ with the data type number. Then we have one function doSomething() which is accepting point object of type Point interface.
interface Point { x: Number, y: Number } let doSomething = function(point: Point) { console.log('Point X: '+point.x); console.log('Point Y: '+point.y); } doSomething({x:1,y:2});
At the last line we have calling function doSomething() with the object.
How to create object in Typescript ?
As in this case we have two variables inside class or interface of type number.
{
x:1,
y:2
}
Now run this code using node.
PS C:\Users\AnilKumar\ts-hello> tsc code-main.ts PS C:\Users\AnilKumar\ts-hello> node code-main.js Point X: 1 Point Y: 2 PS C:\Users\AnilKumar\ts-hello>
Leave a Reply