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

PDF
Getting Started with TypeScript
Gil Fink
 
PPTX
Type script - advanced usage and practices
Iwan van der Kleijn
 
PPTX
TypeScript
Udaiappa Ramachandran
 
PPTX
Typescript ppt
akhilsreyas
 
PPTX
Typescript in 30mins
Udaya Kumar
 
PDF
TypeScript: coding JavaScript without the pain
Sander Mak (@Sander_Mak)
 
PPTX
TypeScript 101
rachelterman
 
Getting Started with TypeScript
Gil Fink
 
Type script - advanced usage and practices
Iwan van der Kleijn
 
Typescript ppt
akhilsreyas
 
Typescript in 30mins
Udaya Kumar
 
TypeScript: coding JavaScript without the pain
Sander Mak (@Sander_Mak)
 
TypeScript 101
rachelterman
 

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
 
Ad

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
 

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
49784907924775488180_LRN2959_Data_Pump_23ai.pdf
Abilash868456
 
PPTX
slidesgo-unlocking-the-code-the-dynamic-dance-of-variables-and-constants-2024...
kr2589474
 
PPTX
PFAS Reporting Requirements 2026 Are You Submission Ready Certivo.pptx
Certivo Inc
 
PDF
Teaching Reproducibility and Embracing Variability: From Floating-Point Exper...
University of Rennes, INSA Rennes, Inria/IRISA, CNRS
 
PDF
Protecting the Digital World Cyber Securit
dnthakkar16
 
PDF
PFAS Reporting Requirements 2026 Are You Submission Ready Certivo.pdf
Certivo Inc
 
PPTX
Web Testing.pptx528278vshbuqffqhhqiwnwuq
studylike474
 
PPTX
Visualising Data with Scatterplots in IBM SPSS Statistics.pptx
Version 1 Analytics
 
PDF
lesson-2-rules-of-netiquette.pdf.bshhsjdj
jasmenrojas249
 
PPTX
Presentation of Computer CLASS 2 .pptx
darshilchaudhary558
 
PDF
Jenkins: An open-source automation server powering CI/CD Automation
SaikatBasu37
 
PPTX
TestNG for Java Testing and Automation testing
ssuser0213cb
 
PDF
Bandai Playdia The Book - David Glotz
BluePanther6
 
PPTX
ConcordeApp: Engineering Global Impact & Unlocking Billions in Event ROI with AI
chastechaste14
 
PDF
advancepresentationskillshdhdhhdhdhdhhfhf
jasmenrojas249
 
PDF
Wondershare Filmora 14.5.20.12999 Crack Full New Version 2025
gsgssg2211
 
PDF
IEEE-CS Tech Predictions, SWEBOK and Quantum Software: Towards Q-SWEBOK
Hironori Washizaki
 
PDF
How to Seamlessly Integrate Salesforce Data Cloud with Marketing Cloud.pdf
NSIQINFOTECH
 
PDF
Why Use Open Source Reporting Tools for Business Intelligence.pdf
Varsha Nayak
 
PPTX
Presentation about variables and constant.pptx
safalsingh810
 
49784907924775488180_LRN2959_Data_Pump_23ai.pdf
Abilash868456
 
slidesgo-unlocking-the-code-the-dynamic-dance-of-variables-and-constants-2024...
kr2589474
 
PFAS Reporting Requirements 2026 Are You Submission Ready Certivo.pptx
Certivo Inc
 
Teaching Reproducibility and Embracing Variability: From Floating-Point Exper...
University of Rennes, INSA Rennes, Inria/IRISA, CNRS
 
Protecting the Digital World Cyber Securit
dnthakkar16
 
PFAS Reporting Requirements 2026 Are You Submission Ready Certivo.pdf
Certivo Inc
 
Web Testing.pptx528278vshbuqffqhhqiwnwuq
studylike474
 
Visualising Data with Scatterplots in IBM SPSS Statistics.pptx
Version 1 Analytics
 
lesson-2-rules-of-netiquette.pdf.bshhsjdj
jasmenrojas249
 
Presentation of Computer CLASS 2 .pptx
darshilchaudhary558
 
Jenkins: An open-source automation server powering CI/CD Automation
SaikatBasu37
 
TestNG for Java Testing and Automation testing
ssuser0213cb
 
Bandai Playdia The Book - David Glotz
BluePanther6
 
ConcordeApp: Engineering Global Impact & Unlocking Billions in Event ROI with AI
chastechaste14
 
advancepresentationskillshdhdhhdhdhdhhfhf
jasmenrojas249
 
Wondershare Filmora 14.5.20.12999 Crack Full New Version 2025
gsgssg2211
 
IEEE-CS Tech Predictions, SWEBOK and Quantum Software: Towards Q-SWEBOK
Hironori Washizaki
 
How to Seamlessly Integrate Salesforce Data Cloud with Marketing Cloud.pdf
NSIQINFOTECH
 
Why Use Open Source Reporting Tools for Business Intelligence.pdf
Varsha Nayak
 
Presentation about variables and constant.pptx
safalsingh810
 

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