Skip to content

Instantly share code, notes, and snippets.

@ragmha
Created November 26, 2018 11:40
Show Gist options
  • Save ragmha/23021a172d0cea7ff6adba7a4eef8214 to your computer and use it in GitHub Desktop.
Save ragmha/23021a172d0cea7ff6adba7a4eef8214 to your computer and use it in GitHub Desktop.
interface Sizes {
sizes: string[];
}
interface Pizza extends Sizes {
name: string;
toppings?: number;
getAvailableSizes(): void;
[key: number]: string; // TypeScript index signatures must either be a ´string´ or a ´number´
}
let pizza: Pizza;
function createPizza(name: string, sizes: string[]): Pizza {
return {
name,
sizes,
getAvailableSizes() {
return this.sizes;
},
};
}
pizza = createPizza("Pepperoni", ["small", "medium", "large"]);
pizza[1] = "PizzaHut";
pizza.toppings = 2;
console.log(`And the Pizza is => ${JSON.stringify(pizza)}`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment