include ../../../../_includes/_util-fns
:marked
Everything that we can do in Angular in TypeScript, we can also do
in JavaScript. Translating from one language to the other is mostly a
matter of changing the way we organize our code and the way we access
Angular APIs.
Since TypeScript is a popular language option in Angular, many of the
code examples you see on the Internet as well as on this site are written
in TypeScript. This cookbook contains recipes for translating these kinds of
code examples to ES5, so that they can be applied to Angular JavaScript
applications.
:marked
## Table of contents
[From TS to ES6 to ES5](#from-ts)
[Modularity: imports and exports](#modularity)
[Classes and Class Metadata](#class-metadata)
[Input and Output Metadata](#property-metadata)
[Dependency Injection](#dependency-injection)
[Host and Query Metadata](#host-query-metadata)
**Run and compare the live TypeScript and JavaScript
code shown in this cookbook.**
a(id="from-ts")
.l-main-section
:marked
## From TS to ES6 to ES5
Since TypeScript is a superset of ES6 JavaScript, and ES6 itself is a superset of ES5, the
transformation of Typescript code all the way to ES5 javascript can be seen as "shedding"
features.
When going from TypeScript to ES6 with decorators, we mostly remove
[type annotations](https://www.typescriptlang.org/docs/handbook/basic-types.html)
, such as `string` or `boolean`, and
[class property modifiers](http://www.typescriptlang.org/docs/handbook/classes.html#public-private-and-protected-modifiers)
such as `public` and `private`.
The exception is type annotations used for dependency injection, which can be kept.
Going from ES6 with decorators to plain ES6 JavaScript we lose all
[decorators](https://www.typescriptlang.org/docs/handbook/decorators.html)
and the remaining type annotations.
We also lose class properties, which now have to be declared in the class constructor.
Finally, in the transition from ES6 to ES5 JavaScript the main missing features are `import`
statements and `class` declarations.
For ES6 transpilation we recommend using a similar setup as our TypeScript quickstart,
replacing TypeScript with [Babel](https://babeljs.io/) using the `es2015` preset.
To use decorators and annotations with Babel, install the
[`angular2`](https://github.com/shuhei/babel-plugin-angular2-annotations) preset as well.
a(id="modularity")
.l-main-section
:marked
## Importing and Exporting
### Importing Angular Code
In TypeScript and ES6 JavaScript, Angular classes, functions, and other members are imported
with ES6 `import` statements.
In ES5 JavaScript code, when using [the Angular packages](../glossary.html#!#scoped-package),
we can access Angular code through the global `ng` object. In the nested members of this
object we'll find everything we would import from `@angular` in TypeScript:
+makeTabs(`
cb-ts-to-js/ts/app/main.ts,
cb-ts-to-js/js-es6-decorators/app/main.es6,
cb-ts-to-js/js-es6/app/main.es6,
cb-ts-to-js/js/app/main.js
`,`
ng2import,
ng2import,
ng2import,
ng2import
`,`
Typescript,
ES6 JavaScript with decorators,
ES6 JavaScript,
ES5 JavaScript
`)
:marked
### Importing and Exporting Application Code
Each file in an Angular TypeScript or ES6 JavaScript application constitutes a ES6 module.
When we want to make something from a module available to other modules, we `export` it.
In an Angular ES5 JavaScript application, we load each file to the page using a `