- Learn React with TypeScript 3
- Carl Rippon
- 162字
- 2021-06-10 19:16:37
Importing
Importing allows us to import items from an exported module. We do this using an import statement that includes the item names to import in curly braces and the file path to get the items from (excluding the ts extension). We can only import items that are exported in the other module file.
- Let's resolve the issue with our OrderDetail class by importing the Product interface:
import { Product } from "./product";
class OrderDetail {
product: Product;
quantity: number;
getTotal(discount: number): number {
const priceWithoutDiscount = this.product.unitPrice * this.quantity;
const discountAmount = priceWithoutDiscount * discount;
return priceWithoutDiscount - discountAmount;
}
}
- We can rename imported items using the as keyword in an import statement. We then reference the item in our code using the new name:
import { Product as Stock } from "./product";
class OrderDetail {
product: Stock;
quantity: number;
getTotal(discount: number): number {
const priceWithoutDiscount = this.product.unitPrice * this.quantity;
const discountAmount = priceWithoutDiscount * discount;
return priceWithoutDiscount - discountAmount;
}
}
推薦閱讀
- The Android Game Developer's Handbook
- WebAssembly實(shí)戰(zhàn)
- Developing Mobile Web ArcGIS Applications
- 云原生Spring實(shí)戰(zhàn)
- Silverlight魔幻銀燈
- 微信小程序開發(fā)解析
- iOS編程基礎(chǔ):Swift、Xcode和Cocoa入門指南
- Drupal 8 Configuration Management
- HTML5 APP開發(fā)從入門到精通(微課精編版)
- Mastering ArcGIS Enterprise Administration
- 深入理解Kafka:核心設(shè)計(jì)與實(shí)踐原理
- 金融商業(yè)數(shù)據(jù)分析:基于Python和SAS
- 優(yōu)化驅(qū)動的設(shè)計(jì)方法
- C語言程序設(shè)計(jì)實(shí)驗(yàn)指導(dǎo)教程
- C/C++語言程序開發(fā)參考手冊