Skip to content
This repository has been archived by the owner on Feb 20, 2019. It is now read-only.

TypeScript guidelines

dzearing edited this page Nov 25, 2014 · 5 revisions

Filename / content guidelines

There are two ways to export things in a typescript file:

You can export many things:

Things.ts:

export class Foo { ... }
export class Bar { ... }

...or you can export only one thing using the = syntax:

Foo.ts:

class Foo { ... }
export = Foo;

The difference lies in consumption. In the first, to use Foo, you'd have to load Things first, and then know that Foo is available on Things:

import Things = require('Things');
var foo = new Things.Foo();

In the second model, there is only one Foo and it is straightforward to understand how to load it:

import Foo = require('Foo');
var foo = new Foo();

We should try to adhere to one class/interface per TS file. Not only is it more obvious about what's being loaded, but it keeps file sizes smaller and thus more maintainable.

## Interfaces

Interfaces should be in their own files. Interface names should be prefixed with I:

```typescript
// Good (filename: IDataSource.ts)
interface IDataSource { ... }

// Bad (filename: DataSource.ts)
interface DataSource { ... }

Access modifiers

With TypeScript 1.3 we now have protected support, which makes access modifiers (coupled with subclassing) very useful. It is recommended to use them as appropriate. However, they do not replace using underscore prefixes for protected/private things. Remember that TypeScript, and thus access modifiers, are a precompile check and that they don't translate into a consumed JavaScript library, so we still need a way to differentiate public methods from internals.

Clone this wiki locally