This post shows how to use interface for Array types in TypeScript with an example.
In TypeScript, interfaces allow us to create contracts other classes/ objects have to implement. We can use them to define custom types without creating classes.
Read the complete tutorial at https://www.javaguides.net/2019/10/typescript-interface-tutorial-with-examples.html.
In TypeScript, interfaces allow us to create contracts other classes/ objects have to implement. We can use them to define custom types without creating classes.
Read the complete tutorial at https://www.javaguides.net/2019/10/typescript-interface-tutorial-with-examples.html.
Interface for Array Type Example
An interface can also define the type of an array where you can define the type of index as well as values
interface NumList {
[index:number]:number
}
let numArr: NumList = [1, 2, 3];
numArr[0];
numArr[1];
console.log(numArr);
// Array which return string
interface ProLangArray {
[index:number]:string
}
// use of the interface
let progLangArray : ProLangArray = ['C', 'C++', 'Java', 'Python'];
console.log(progLangArray);
Output:
[ 1, 2, 3 ]
[ 'C', 'C++', 'Java', 'Python' ]
Learn typescript at TypeScript Tutorial with Examples.
Comments
Post a Comment