Skip to content

Commit

Permalink
Update documentation, see #170
Browse files Browse the repository at this point in the history
  • Loading branch information
samreid committed Nov 10, 2021
1 parent d5cef04 commit 29db6ca
Showing 1 changed file with 39 additions and 7 deletions.
46 changes: 39 additions & 7 deletions doc/typescript-conventions.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,61 @@ For example:

```ts
// Recommended: infers type x:number
const x = 7;
const x = 7;

// Not recommended, the type information is redundant.
const x: number = 7;
```

However, if there is a complicated or volatile (api hasn't stabilized) expression on the right hand side, it may be valuable on rare occasions to
specify the type manually:
However, if there is a complicated or volatile (api hasn't stabilized) expression on the right hand side, it may be
valuable on rare occasions to specify the type manually:

```ts
// OK to specify the type manually in complex or volatile cases
const x: number = someComplicatedExpressionOrVolatileStatementThatHasntStabilized();
```

This same principle applies to type parameters. For instance, TypeScript can infer the parametric type of `new Property`
This same principle applies to type parameters. For instance, TypeScript can infer the parametric type of `new Property`
based on the value of the first parameter.

```ts
// Recommended
new Property(new Laser());
new Property( new Laser() );

// Not recommended, type information is redundant
new Property<Laser>(new Laser());
new Property<Laser>( new Laser() );
```

Again, in complex or volatile cases, at the developer preference, the redundant type annotations may prove useful.
Again, in complex or volatile cases, at the developer preference, the redundant type annotations may prove useful.

### Visibility Annotations

In TypeScript, the default visibility (if unspecified) is `public`. For methods, attributes, constructors, etc which are
intended to be public, the visibility modifier can be omitted. Or at the developer's discretion, `public` can be
specified.

```ts
class Clock {
time: number; // ok
public age: number; // also ok
}
```

### Enumerations

Please see https://github.com/phetsims/wilder/blob/master/js/wilder/model/WilderEnumerationsTypescriptTestModel.ts

### Options and Config

Please see https://github.com/phetsims/wilder/blob/master/js/wilder/model/WilderOptionsTypescriptTestModel.ts

### Syntax in Type Declarations

We prefer to follow the TypeScript handbook and put semicolon delimiters in type declarations:

```ts
type Cat = {
person: Person; // note the semicolons
age: number;
}
```

0 comments on commit 29db6ca

Please sign in to comment.