What is TypeScript ?
TypeScript is a strongly typed programming language that builds on JavaScript, giving you better tooling at any scale.
What are interfaces ?
TypeScript interfaces are a powerful feature that allows you to define a contrcat for objects and function in code.
They provide a way to ensure that your code is type-safe, reducing the possibility of runtime errors and increasing the overall quality of your code.
Basic Interface.
At its core, an interfaces is simply a names set of properities that describe the shape of an object.
interfaces Car {
name:string,
model:string,
color:string
}
Using Interfaces
You can use interfaces to define the type of an object,
interface Car {
name:string,
model:string,
color:string,
}
const car : Car={
name: 'Hyundai',
model: '2000'
}
Functional Interfaces
Interfaces can also be used to define the shape of functions. Function interfaces describe the types of the arguments and returns the value for a function.
interface MathFunc {
(x: number, y: number): number
}
Using Functional Interfaces
You can use function interfaces to define a function, like this,
const add: Mathfunc = (x,y) => x+y;
Extending Interfaces
Interfaces can also be extended to add additional functionality or properties.
When you extend an interface, the resulting interface inherits all of the properties and methods of the original interface and can also define its own properties and methods.
interface Animal {
name: string,
age: string,
}
interface Dog extends Animal {
breed: string,
}
Using the interface
const myDog : Dog = {
name: 'Rufus',
age: 3,
breed: 'Lab',
};
If you liked my blog, please do follow me on Rhea RB
Thank you for Reading ! Happy Coding !!