SlideShare a Scribd company logo
TYPESCRIPT
MODULES
Noam Kfir
■ SeniorArchitect at Sela
■ Front-End.IL Meetup organizer
■ Telerik Developer Expert
■ @NoamKfir
■ noam@kfir.cc
WhatAre Modules
■ Prevent name collisions
■ Group constructs logically
– Organization
– Namespacing
– Encapsulation
■ Implemented as scoped JavaScript objects
Superset of JavaScript
■ TypeScript has to work where JavaScript works
■ But module definitions differ:
CommonJS
Node.js
AMD
RequireJS
ECMAScript
6/2015
SystemJS
Custom
Modules
Different JS
Versions
Internal Modules
Represent namespaces
• The module name unrelated to file name
• Can be nested
Provide scope
• Declarations inside the module are private
• Can be exposed with the export keyword
Internal Modules - Syntax
module Internal {
export class B extends A {
a: A = new A();
}
}
Internal Modules -Type
Information
■ The compiler needs to know where to find the type info
/// <reference path="source.ts" />
– Compiler follows references, determines order
■ Or use tsconfig.json to create aTypeScript project
– Automatically sees all files in the directory
Internal Modules - Merging
■ Multiple files can define the same module
■ The compiler merges the individual modules
■ Scope is determined by original unmerged module
– Not shared
External Modules
Represent grouped constructs
• Module name defined by file name
• Don't need namespaces
Provide scope
• Declarations inside the module are private
• Can be exposed with the export keyword
Module Loaders
■ TypeScript doesn’t implement the module system itself
■ Uses module loaders instead
■ Unifies module declaration for external module loaders
■ Available loaders:
commonjs amd umd system es6
External Modules - Syntax
import m = require('mod');
export var t = m.something + 1;
Transpiled to AMD
define(['require', 'exports', 'mod'],
function(require, exports, m) {
exports.t = m.something + 1;
}
);
Transpiled to CommonJS
var m = require('mod');
exports.t = m.something + 1;
Aliases
■ Aliases are just shortcuts
■ Help shorted access to nested constructs
■ Can’t be combined with regular import
import foo = mod.foo;
class B {
a: A = foo;
}
Export = Syntax
■ External module syntax can be cumbersome
■ Export = syntax exports a single unqualified value
– Class, interface, module, function, enum
import A = require('./A');
class B {
a: A = new A();
}
export = B
ES6 Modules
■ External modules using ES6 syntax
■ More succinct than the regular external module syntax
■ More flexible than the the export = syntax
ES6 Modules – Syntax
• Exporting (from “A.ts”)
export class A {}
• Importing (to “B.ts”)
import { A } from './A';
export class B {
a: A = new A();
}
ES6 Modules – Default
Members
• Exporting (from “A.ts”)
export default class {}
• Importing (to “B.ts”)
import A from './A';
export class B {
a: A = new A();
}
Optional Module Loading
■ require() emitted only if a module is actually used at run time
■ If only type info is needed, require() isn’t emitted
■ Useful for type safety
Ambient Modules
■ Modules defined in type definition files – .d.ts
■ Provide type info for non-TypeScript files
■ Can be internal or external
■ Internal – mainly for client scripts
■ External –helps build larger definitions in one file
Ambient Internal Module –
D3.d.ts (simplified excerpt)
declare module D3 {
export interface Selectors {
select: { (selector: string): Selection; }
}
export interface Event {
x: number;
y: number;
}
}
declare var d3: D3.Base;
Ambient External Module –
node.d.ts (simplified extract)
declare module "url" {
export interface Url {
protocol?: string; hostname?: string;
}
export function parse(urlStr: string, …): Url;
}
declare module "path" {
export function join(...paths: any[]): string;
}
.ts file
• import x = require("name");
• top-level import/export declarations
.d.ts file
• like #1
• declaration file with top-level import/export
Ambient external module declaration
• find module by quoted name
LocatingType Info
Declaration Merging
■ Same kind
– module, class, interface, function, value
■ Different kinds
– module/class
– module/function
– module/enum
THANKYOU!
Noam Kfir
@NoamKfir
noam@kfir.cc

More Related Content

What's hot (20)

PPTX
TypeScript: Basic Features and Compilation Guide
Nascenia IT
 
PPTX
Introducing type script
Remo Jansen
 
PPTX
Typescript Fundamentals
Sunny Sharma
 
PPTX
TypeScript intro
Ats Uiboupin
 
PPTX
TypeScript . the JavaScript developer best friend!
Alessandro Giorgetti
 
PDF
Power Leveling your TypeScript
Offirmo
 
PPTX
AngularConf2015
Alessandro Giorgetti
 
PDF
TypeScript Best Practices
felixbillon
 
PDF
TypeScript introduction to scalable javascript application
Andrea Paciolla
 
PPT
Learning typescript
Alexandre Marreiros
 
PPTX
TypeScript
Oswald Campesato
 
PPTX
Introduction about type script
Binh Quan Duc
 
PPTX
Typescript 101 introduction
Bob German
 
PDF
Introduction to TypeScript by Winston Levi
Winston Levi
 
PPTX
Typescript
Nikhil Thomas
 
PPTX
Getting started with typescript
C...L, NESPRESSO, WAFAASSURANCE, SOFRECOM ORANGE
 
PPT
TypeScript Presentation
Patrick John Pacaña
 
PDF
TypeScript - An Introduction
NexThoughts Technologies
 
PDF
TypeScript: Angular's Secret Weapon
Laurent Duveau
 
PPTX
TypeScript Overview
Aniruddha Chakrabarti
 
TypeScript: Basic Features and Compilation Guide
Nascenia IT
 
Introducing type script
Remo Jansen
 
Typescript Fundamentals
Sunny Sharma
 
TypeScript intro
Ats Uiboupin
 
TypeScript . the JavaScript developer best friend!
Alessandro Giorgetti
 
Power Leveling your TypeScript
Offirmo
 
AngularConf2015
Alessandro Giorgetti
 
TypeScript Best Practices
felixbillon
 
TypeScript introduction to scalable javascript application
Andrea Paciolla
 
Learning typescript
Alexandre Marreiros
 
TypeScript
Oswald Campesato
 
Introduction about type script
Binh Quan Duc
 
Typescript 101 introduction
Bob German
 
Introduction to TypeScript by Winston Levi
Winston Levi
 
Typescript
Nikhil Thomas
 
Getting started with typescript
C...L, NESPRESSO, WAFAASSURANCE, SOFRECOM ORANGE
 
TypeScript Presentation
Patrick John Pacaña
 
TypeScript - An Introduction
NexThoughts Technologies
 
TypeScript: Angular's Secret Weapon
Laurent Duveau
 
TypeScript Overview
Aniruddha Chakrabarti
 

Viewers also liked (19)

PPTX
Why TypeScript?
FITC
 
PPTX
ProtractorJS for automated testing of Angular 1.x/2.x applications
Binary Studio
 
PPTX
Better End-to-End Testing with Page Objects Model using Protractor
Kasun Kodagoda
 
PPT
Protractor powerpoint
lindarousselle
 
PDF
Introduction to Protractor
Florian Fesseler
 
PDF
TypeScript Introduction
Dmitry Sheiko
 
PDF
Apresentação de Padrões de Design para Aplicativos Móveis.
Hewerson Freitas
 
PDF
Protractor: Tips & Tricks
Sergey Bolshchikov
 
PDF
Introduction to Protractor
Jie-Wei Wu
 
PDF
Javascript test frameworks
talkitbr
 
PDF
Angular 2 - Core Concepts
Fabio Biondi
 
ODP
Introduction to Angular 2
Knoldus Inc.
 
PDF
Getting Started with Angular 2
FITC
 
PPTX
Using a protractor
fknights
 
PDF
Introduction à Angular 2
Laurent Duveau
 
DOCX
Rescue.asd
jul helmi
 
DOCX
Mt on leadership and its effects on employees performance
Shashi Chandra
 
PDF
Rapport Doing Business 2015
Franck Dasilva
 
PDF
Geohash
Narayan Kandel
 
Why TypeScript?
FITC
 
ProtractorJS for automated testing of Angular 1.x/2.x applications
Binary Studio
 
Better End-to-End Testing with Page Objects Model using Protractor
Kasun Kodagoda
 
Protractor powerpoint
lindarousselle
 
Introduction to Protractor
Florian Fesseler
 
TypeScript Introduction
Dmitry Sheiko
 
Apresentação de Padrões de Design para Aplicativos Móveis.
Hewerson Freitas
 
Protractor: Tips & Tricks
Sergey Bolshchikov
 
Introduction to Protractor
Jie-Wei Wu
 
Javascript test frameworks
talkitbr
 
Angular 2 - Core Concepts
Fabio Biondi
 
Introduction to Angular 2
Knoldus Inc.
 
Getting Started with Angular 2
FITC
 
Using a protractor
fknights
 
Introduction à Angular 2
Laurent Duveau
 
Rescue.asd
jul helmi
 
Mt on leadership and its effects on employees performance
Shashi Chandra
 
Rapport Doing Business 2015
Franck Dasilva
 
Ad

Similar to TypeScript Modules (20)

PPTX
Type-Script-Fundamentals Type-Script-Fundamentals
ssuser742f291
 
PDF
Typescript for the programmers who like javascript
Andrei Sebastian Cîmpean
 
PPTX
Type script
Mallikarjuna G D
 
PPTX
JavaScript Module Loaders
zeroproductionincidents
 
PDF
Introduction to TypeScript
NexThoughts Technologies
 
PPTX
Why do we need TypeScript?
Nitay Neeman
 
PPTX
Rits Brown Bag - TypeScript
Right IT Services
 
PDF
Typescript For Beginners The Ultimate Guide Sufyan Bin Uzayr
baielldebove
 
PDF
Type script
srinivaskapa1
 
PPTX
Typescript language extension of java script
michaelaaron25322
 
PPTX
All You Need to Know About Type Script
Folio3 Software
 
PDF
Large Scale JavaScript with TypeScript
Oliver Zeigermann
 
PDF
Modular JavaScript
NLJUG
 
PDF
Unbundling the JavaScript module bundler - Luciano Mammino - Codemotion Rome ...
Codemotion
 
PDF
Unbundling the JavaScript module bundler - Codemotion Rome 2018
Luciano Mammino
 
PPTX
Generate typings from JavaScript with TypeScript 3.7
Benny Neugebauer
 
PDF
Reasons to Use Typescript for Your Next Project Over Javascript.pdf
MobMaxime
 
PPTX
Type script
Zunair Shoes
 
PPTX
Typescript tips & tricks
Ori Calvo
 
PDF
One language to rule them all type script
Gil Fink
 
Type-Script-Fundamentals Type-Script-Fundamentals
ssuser742f291
 
Typescript for the programmers who like javascript
Andrei Sebastian Cîmpean
 
Type script
Mallikarjuna G D
 
JavaScript Module Loaders
zeroproductionincidents
 
Introduction to TypeScript
NexThoughts Technologies
 
Why do we need TypeScript?
Nitay Neeman
 
Rits Brown Bag - TypeScript
Right IT Services
 
Typescript For Beginners The Ultimate Guide Sufyan Bin Uzayr
baielldebove
 
Type script
srinivaskapa1
 
Typescript language extension of java script
michaelaaron25322
 
All You Need to Know About Type Script
Folio3 Software
 
Large Scale JavaScript with TypeScript
Oliver Zeigermann
 
Modular JavaScript
NLJUG
 
Unbundling the JavaScript module bundler - Luciano Mammino - Codemotion Rome ...
Codemotion
 
Unbundling the JavaScript module bundler - Codemotion Rome 2018
Luciano Mammino
 
Generate typings from JavaScript with TypeScript 3.7
Benny Neugebauer
 
Reasons to Use Typescript for Your Next Project Over Javascript.pdf
MobMaxime
 
Type script
Zunair Shoes
 
Typescript tips & tricks
Ori Calvo
 
One language to rule them all type script
Gil Fink
 
Ad

More from Noam Kfir (16)

PPTX
Agile Mind Games and the Art of Self-Delusion
Noam Kfir
 
PPTX
Testers and Coders - Blurring the Lines
Noam Kfir
 
PPTX
TDD and the Legacy Code Black Hole
Noam Kfir
 
PPTX
There Is No JavaScript
Noam Kfir
 
PPTX
Angular on ASP.NET MVC 6
Noam Kfir
 
PPTX
Meteor
Noam Kfir
 
PPTX
Clean code
Noam Kfir
 
PPTX
Maximizing UI Automation – A Case Study
Noam Kfir
 
PPTX
Web components
Noam Kfir
 
PPTX
HTML5 and the Evolution of the Web
Noam Kfir
 
PPTX
Git Workflows
Noam Kfir
 
PPTX
Getting Started with Git: A Primer for SVN and TFS Users
Noam Kfir
 
PPTX
Building Cross-Platform JavaScript Apps using Cordova
Noam Kfir
 
PPTX
Telerik Platform
Noam Kfir
 
PPTX
Profiling JavaScript Performance
Noam Kfir
 
PPTX
Drawing in HTML5 Open House
Noam Kfir
 
Agile Mind Games and the Art of Self-Delusion
Noam Kfir
 
Testers and Coders - Blurring the Lines
Noam Kfir
 
TDD and the Legacy Code Black Hole
Noam Kfir
 
There Is No JavaScript
Noam Kfir
 
Angular on ASP.NET MVC 6
Noam Kfir
 
Meteor
Noam Kfir
 
Clean code
Noam Kfir
 
Maximizing UI Automation – A Case Study
Noam Kfir
 
Web components
Noam Kfir
 
HTML5 and the Evolution of the Web
Noam Kfir
 
Git Workflows
Noam Kfir
 
Getting Started with Git: A Primer for SVN and TFS Users
Noam Kfir
 
Building Cross-Platform JavaScript Apps using Cordova
Noam Kfir
 
Telerik Platform
Noam Kfir
 
Profiling JavaScript Performance
Noam Kfir
 
Drawing in HTML5 Open House
Noam Kfir
 

Recently uploaded (20)

PDF
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
PDF
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
PDF
Linux Certificate of Completion - LabEx Certificate
VICTOR MAESTRE RAMIREZ
 
PDF
iTop VPN With Crack Lifetime Activation Key-CODE
utfefguu
 
PDF
Executive Business Intelligence Dashboards
vandeslie24
 
PPTX
How Apagen Empowered an EPC Company with Engineering ERP Software
SatishKumar2651
 
PPTX
Fundamentals_of_Microservices_Architecture.pptx
MuhammadUzair504018
 
PDF
Revenue streams of the Wazirx clone script.pdf
aaronjeffray
 
PDF
Streamline Contractor Lifecycle- TECH EHS Solution
TECH EHS Solution
 
PPTX
Platform for Enterprise Solution - Java EE5
abhishekoza1981
 
PDF
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
PDF
Mobile CMMS Solutions Empowering the Frontline Workforce
CryotosCMMSSoftware
 
PDF
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
PDF
Beyond Binaries: Understanding Diversity and Allyship in a Global Workplace -...
Imma Valls Bernaus
 
PDF
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
PPTX
Comprehensive Guide: Shoviv Exchange to Office 365 Migration Tool 2025
Shoviv Software
 
PDF
Thread In Android-Mastering Concurrency for Responsive Apps.pdf
Nabin Dhakal
 
PPTX
Java Native Memory Leaks: The Hidden Villain Behind JVM Performance Issues
Tier1 app
 
PPTX
Equipment Management Software BIS Safety UK.pptx
BIS Safety Software
 
PPTX
MailsDaddy Outlook OST to PST converter.pptx
abhishekdutt366
 
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
Linux Certificate of Completion - LabEx Certificate
VICTOR MAESTRE RAMIREZ
 
iTop VPN With Crack Lifetime Activation Key-CODE
utfefguu
 
Executive Business Intelligence Dashboards
vandeslie24
 
How Apagen Empowered an EPC Company with Engineering ERP Software
SatishKumar2651
 
Fundamentals_of_Microservices_Architecture.pptx
MuhammadUzair504018
 
Revenue streams of the Wazirx clone script.pdf
aaronjeffray
 
Streamline Contractor Lifecycle- TECH EHS Solution
TECH EHS Solution
 
Platform for Enterprise Solution - Java EE5
abhishekoza1981
 
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
Mobile CMMS Solutions Empowering the Frontline Workforce
CryotosCMMSSoftware
 
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
Beyond Binaries: Understanding Diversity and Allyship in a Global Workplace -...
Imma Valls Bernaus
 
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
Comprehensive Guide: Shoviv Exchange to Office 365 Migration Tool 2025
Shoviv Software
 
Thread In Android-Mastering Concurrency for Responsive Apps.pdf
Nabin Dhakal
 
Java Native Memory Leaks: The Hidden Villain Behind JVM Performance Issues
Tier1 app
 
Equipment Management Software BIS Safety UK.pptx
BIS Safety Software
 
MailsDaddy Outlook OST to PST converter.pptx
abhishekdutt366
 

TypeScript Modules

  • 2. Noam Kfir ■ SeniorArchitect at Sela ■ Front-End.IL Meetup organizer ■ Telerik Developer Expert ■ @NoamKfir ■ [email protected]
  • 3. WhatAre Modules ■ Prevent name collisions ■ Group constructs logically – Organization – Namespacing – Encapsulation ■ Implemented as scoped JavaScript objects
  • 4. Superset of JavaScript ■ TypeScript has to work where JavaScript works ■ But module definitions differ: CommonJS Node.js AMD RequireJS ECMAScript 6/2015 SystemJS Custom Modules Different JS Versions
  • 5. Internal Modules Represent namespaces • The module name unrelated to file name • Can be nested Provide scope • Declarations inside the module are private • Can be exposed with the export keyword
  • 6. Internal Modules - Syntax module Internal { export class B extends A { a: A = new A(); } }
  • 7. Internal Modules -Type Information ■ The compiler needs to know where to find the type info /// <reference path="source.ts" /> – Compiler follows references, determines order ■ Or use tsconfig.json to create aTypeScript project – Automatically sees all files in the directory
  • 8. Internal Modules - Merging ■ Multiple files can define the same module ■ The compiler merges the individual modules ■ Scope is determined by original unmerged module – Not shared
  • 9. External Modules Represent grouped constructs • Module name defined by file name • Don't need namespaces Provide scope • Declarations inside the module are private • Can be exposed with the export keyword
  • 10. Module Loaders ■ TypeScript doesn’t implement the module system itself ■ Uses module loaders instead ■ Unifies module declaration for external module loaders ■ Available loaders: commonjs amd umd system es6
  • 11. External Modules - Syntax import m = require('mod'); export var t = m.something + 1;
  • 12. Transpiled to AMD define(['require', 'exports', 'mod'], function(require, exports, m) { exports.t = m.something + 1; } );
  • 13. Transpiled to CommonJS var m = require('mod'); exports.t = m.something + 1;
  • 14. Aliases ■ Aliases are just shortcuts ■ Help shorted access to nested constructs ■ Can’t be combined with regular import import foo = mod.foo; class B { a: A = foo; }
  • 15. Export = Syntax ■ External module syntax can be cumbersome ■ Export = syntax exports a single unqualified value – Class, interface, module, function, enum import A = require('./A'); class B { a: A = new A(); } export = B
  • 16. ES6 Modules ■ External modules using ES6 syntax ■ More succinct than the regular external module syntax ■ More flexible than the the export = syntax
  • 17. ES6 Modules – Syntax • Exporting (from “A.ts”) export class A {} • Importing (to “B.ts”) import { A } from './A'; export class B { a: A = new A(); }
  • 18. ES6 Modules – Default Members • Exporting (from “A.ts”) export default class {} • Importing (to “B.ts”) import A from './A'; export class B { a: A = new A(); }
  • 19. Optional Module Loading ■ require() emitted only if a module is actually used at run time ■ If only type info is needed, require() isn’t emitted ■ Useful for type safety
  • 20. Ambient Modules ■ Modules defined in type definition files – .d.ts ■ Provide type info for non-TypeScript files ■ Can be internal or external ■ Internal – mainly for client scripts ■ External –helps build larger definitions in one file
  • 21. Ambient Internal Module – D3.d.ts (simplified excerpt) declare module D3 { export interface Selectors { select: { (selector: string): Selection; } } export interface Event { x: number; y: number; } } declare var d3: D3.Base;
  • 22. Ambient External Module – node.d.ts (simplified extract) declare module "url" { export interface Url { protocol?: string; hostname?: string; } export function parse(urlStr: string, …): Url; } declare module "path" { export function join(...paths: any[]): string; }
  • 23. .ts file • import x = require("name"); • top-level import/export declarations .d.ts file • like #1 • declaration file with top-level import/export Ambient external module declaration • find module by quoted name LocatingType Info
  • 24. Declaration Merging ■ Same kind – module, class, interface, function, value ■ Different kinds – module/class – module/function – module/enum