typescript modules

25
TYPESCRIPT MODULES

Upload: noam-kfir

Post on 20-Jan-2017

503 views

Category:

Software


3 download

TRANSCRIPT

Page 1: TypeScript Modules

TYPESCRIPT MODULES

Page 2: TypeScript Modules

Noam Kfir

■ Senior Architect at Sela■ Front-End.IL Meetup organizer■ Telerik Developer Expert

■ @NoamKfir■ [email protected]

Page 3: TypeScript Modules

What Are Modules

■ Prevent name collisions

■ Group constructs logically– Organization– Namespacing– Encapsulation

■ Implemented as scoped JavaScript objects

Page 4: TypeScript Modules

Superset of JavaScript

■ TypeScript has to work where JavaScript works

■ But module definitions differ:

CommonJS

Node.jsAMD

RequireJSECMAScript 6/2015 SystemJS

Custom Modules

Different JS

Versions

Page 5: TypeScript Modules

Internal Modules

Represent namespaces• The module name unrelated to file

name• Can be nestedProvide scope• Declarations inside the module are

private• Can be exposed with the export

keyword

Page 6: TypeScript Modules

Internal Modules - Syntaxmodule Internal { export class B extends A { a: A = new A(); }}

Page 7: TypeScript Modules

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 a TypeScript project– Automatically sees all files in the directory

Page 8: TypeScript Modules

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

Page 9: TypeScript Modules

External Modules

Represent grouped constructs• Module name defined by file name• Don't need namespacesProvide scope• Declarations inside the module are

private• Can be exposed with the export

keyword

Page 10: TypeScript Modules

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

Page 11: TypeScript Modules

External Modules - Syntaximport m = require('mod');export var t = m.something + 1;

Page 12: TypeScript Modules

Transpiled to AMD

define(['require', 'exports', 'mod'], function(require, exports, m) { exports.t = m.something + 1; });

Page 13: TypeScript Modules

Transpiled to CommonJS

var m = require('mod');exports.t = m.something + 1;

Page 14: TypeScript Modules

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;}

Page 15: TypeScript Modules

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

Page 16: TypeScript Modules

ES6 Modules

■ External modules using ES6 syntax

■ More succinct than the regular external module syntax

■ More flexible than the the export = syntax

Page 17: TypeScript Modules

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();}

Page 18: TypeScript Modules

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();}

Page 19: TypeScript Modules

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

Page 20: TypeScript Modules

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

Page 21: TypeScript Modules

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;

Page 22: TypeScript Modules

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;}

Page 23: TypeScript Modules

.ts file• import x = require("name");• top-level import/export declarations.d.ts file• like #1• declaration file with top-level

import/exportAmbient external module declaration• find module by quoted name

Locating Type Info

Page 24: TypeScript Modules

Declaration Merging

■ Same kind– module, class, interface, function, value

■ Different kinds– module/class– module/function– module/enum

Page 25: TypeScript Modules

THANK YOU!Noam Kfir

@[email protected]