Import and Export
Import Export
π¨βπΌ Our code is getting large. Let's split it into separate modules to keep
things organized.
// data.ts
export const book = { title: 'TypeScript Tips', pages: 200 }
// index.ts
import { book } from './data.ts'
π¨ Open
and
and:
- Move the
userandproductobjects fromindex.tsintodata.ts - Export them from
data.tsas named exports with these fixtures:user:{ id: '1', name: 'Alice', email: 'alice@example.com' }product:{ id: 'p1', name: 'Laptop', price: 999.99 }
- Import
{ user, product }back intoindex.ts - Export
displayUseranddisplayProductfromindex.ts
Completion check:
index.ts can call both display helpers with the imported
values, and both helpers are exported from the module.π MDN - ES Modules