Created
September 2, 2017 13:49
-
-
Save JaykeOps/9a6bedf1a55aa0936cac5691e19e36b3 to your computer and use it in GitHub Desktop.
Just some notes I took while following along the TypeScript documentation.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//1.0 Interface declared on function | |
function printBirthday(birthday: { Date: Date, Age: number, Name: string}) { | |
console.log(birthday.Name + "'s " + (birthday.Age + 1) + "th birthday" | |
+ " is on " + birthday.Date + "."); | |
} | |
let person = { Name: "Karl", Age: 32, Date: new Date(), Occupation: "Painter" }; | |
printBirthday(person); | |
//1.1 Interface with optional properties | |
interface IOrder { | |
readonly membership?: string; | |
readonly company?: string; | |
product: string; | |
contactName: string; | |
contactPhone: string; | |
} | |
interface IPriceReductionSpecification { | |
memberReduction: number; | |
companyReduction: number; | |
} | |
function calculateOrderTotal(order: IOrder, spec: IPriceReductionSpecification) { | |
let priceReduction = 0; | |
priceReduction += order.membership ? spec.memberReduction : 0; | |
priceReduction += order.company ? spec.companyReduction : 0; | |
let priceReductionInPercentage = priceReduction / 100; | |
let price = order.product == "Claw Hammer" ? 1000 : 0; | |
return price - (price * priceReductionInPercentage); | |
} | |
//Its optional to explicitly implement an interface | |
let order1 = { | |
product: "Claw Hammer", | |
contactName: "Abe Johnson", | |
contactPhone: "0734-552198" | |
}; | |
let order2: IOrder = { | |
membership: "Premium", | |
company: "Arelius Ink.", | |
product: "Claw Hammer", | |
contactName: "Marcus Arelius", | |
contactPhone: "0724-122430" | |
}; | |
let priceReductionSpecification = { | |
memberReduction: 10, | |
companyReduction: 10 | |
}; | |
console.log("Full price: " + calculateOrderTotal(order1, priceReductionSpecification)); | |
console.log("Reduced price: " + calculateOrderTotal(order2, priceReductionSpecification)); | |
//1.2 Readonly Array | |
let numbers: ReadonlyArray<number> = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment