Reputation: 63
// EX 1
interface Animal {
[dwdwdwd: number]: string;
}
let myArr : Animal = ["Joy", "Joy2"];
console.log("EX1 : " + myArr[1]); // Joy2
// EX 2
interface States {
[state: string]: boolean;
}
let myArr2: States = {'enabled': true, 'maximized':false};
console.log("EX2 : " + myArr2['maximized']); // false
// EX 3
interface Animal2 {
(props: { [key: string]: any }): boolean;
}
const test = {props : {'enabled':false}}; // I don't think this is correct of putting variable
let myArr3 : Animal2 = test; // compile error
I want to make make EX 3 so that it utilizes "interface Animal2", but I do not know how to utilize it like the way shown in EX1 and EX2.
Can anyone provide me an example of the use of EX3? and console.log any variable of it. Thank you very much.
Upvotes: 0
Views: 30
Reputation: 442
it looks like you're trying to create an interface for a function:
interface Animal2 {
(props: {[key: string]: any}): boolean;
}
let isPeterPan: Animal2 = (props: {name: string, lastName: string}) => props.name === "peter" && props.lastName === "pan";
console.log(isPeterPan({name: "peter", lastName: "parker"})); // false
console.log(isPeterPan({name: "peter", lastName: "pan"})); // true
Upvotes: 1