Skip to content

Commit b155084

Browse files
Use 'ts' instead of 'TypeScript' to appease every markdown highlighter.
1 parent 7243e8c commit b155084

11 files changed

Lines changed: 152 additions & 152 deletions

pages/Basic Types.md

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ In TypeScript, we support much the same types as you would expected in JavaScrip
77

88
The most basic datatype is the simple true/false value, which JavaScript and TypeScript (as well as other languages) call a `boolean` value.
99

10-
```TypeScript
10+
```ts
1111
var isDone: boolean = false;
1212
```
1313

@@ -17,7 +17,7 @@ As in JavaScript, all numbers in TypeScript are floating point values.
1717
These floating point numbers get the type `number`.
1818
In addition to hexadecimal and decimal literals, TypeScript also supports binary and octal literals introduced in ECMAScript 2015.
1919

20-
```TypeScript
20+
```ts
2121
var decLiteral: number = 6;
2222
var hexLiteral: number = 0x9837abdef;
2323
var binaryLiteral: number = 0b0010;
@@ -30,15 +30,15 @@ Another fundamental part of creating programs in JavaScript for webpages and ser
3030
As in other languages, we use the type `string` to refer to these textual datatypes.
3131
Just like JavaScript, TypeScript also uses the double quote (`"`) or single quote (`'`) to surround string data.
3232

33-
```TypeScript
33+
```ts
3434
var name: string = "bob";
3535
name = 'smith';
3636
```
3737

3838
You can also use *template strings*, which can span multiple lines and have embedded expressions.
3939
These strings are surrounded by the backtick/backquote (`` ` ``) character, and embedded expressions are of the form `${ expr }`
4040

41-
```TypeScript
41+
```ts
4242
var name: string = `Gene`;
4343
var age: number = 37;
4444
var sentence: string = `Hello, my name is ${ name }.
@@ -48,7 +48,7 @@ I'll be ${ age + 1 } years old next month.`
4848

4949
This is equivalent to declaring `sentence` like so:
5050

51-
```TypeScript
51+
```ts
5252
var sentence: string = "Hello, my name is " + name + ".\n\n" +
5353
"I'll be " + (age + 1) + " years old next month."
5454
```
@@ -59,13 +59,13 @@ TypeScript, like JavaScript, allows you to work with arrays of values.
5959
Array types can be written in one of two ways.
6060
In the first, you use the type of the elements followed by `[]` to denote an array of that element type:
6161

62-
```TypeScript
62+
```ts
6363
var list: number[] = [1, 2, 3];
6464
```
6565

6666
The second way uses a generic array type, `Array<elemType>`:
6767

68-
```TypeScript
68+
```ts
6969
var list: Array<number> = [1, 2, 3];
7070
```
7171

@@ -74,7 +74,7 @@ var list: Array<number> = [1, 2, 3];
7474
Tuple types allow you to express an array where the type of a fixed number of elements is known, but need not be the same.
7575
For example, you may want to represent a value as a pair of a `string` and a `number`:
7676

77-
```TypeScript
77+
```ts
7878
// Declare a tuple type
7979
var x: [string, number];
8080
// Initialize it
@@ -85,14 +85,14 @@ x = [10, 'hello']; // Error
8585

8686
When accessing an element with a known index, the correct type is retrieved:
8787

88-
```TypeScript
88+
```ts
8989
console.log(x[0].substr(1)); // OK
9090
console.log(x[1].substr(1)); // Error, 'number' does not have 'substr'
9191
```
9292

9393
When accessing an element outside the set of known indices, a union type is used instead:
9494

95-
```TypeScript
95+
```ts
9696
x[3] = 'world'; // OK, string can be assigned to (string | number)
9797
console.log(x[5].toString()); // OK, 'string' and 'number' both have toString
9898
x[6] = true; // Error, boolean isn't (string | number)
@@ -105,7 +105,7 @@ Union types are an advanced topic that we'll cover in a later chapter.
105105
A helpful addition to the standard set of datatypes from JavaScript is the `enum`.
106106
As in languages like C#, an enum is a way of giving more friendly names to sets of numeric values.
107107

108-
```TypeScript
108+
```ts
109109
enum Color {Red, Green, Blue};
110110
var c: Color = Color.Green;
111111
```
@@ -114,22 +114,22 @@ By default, enums begin numbering their members starting at `0`.
114114
You can change this by manually setting the value of one its members.
115115
For example, we can start the previous example at `1` instead of `0`:
116116

117-
```TypeScript
117+
```ts
118118
enum Color {Red = 1, Green, Blue};
119119
var c: Color = Color.Green;
120120
```
121121

122122
Or, even manually set all the values in the enum:
123123

124-
```TypeScript
124+
```ts
125125
enum Color {Red = 1, Green = 2, Blue = 4};
126126
var c: Color = Color.Green;
127127
```
128128

129129
A handy feature of enums is that you can also go from a numeric value to the name of that value in the enum.
130130
For example, if we had the value `2` but weren't sure what that mapped to in the `Color` enum above, we could look up the corresponding name:
131131

132-
```TypeScript
132+
```ts
133133
enum Color {Red = 1, Green, Blue};
134134
var colorName: string = Color[2];
135135

@@ -143,7 +143,7 @@ These values may come from dynamic content, e.g. from the user or a 3rd party li
143143
In these cases, we want to opt-out of type-checking and let the values pass through compile-time checks.
144144
To do so, we label these with the `any` type:
145145

146-
```TypeScript
146+
```ts
147147
var notSure: any = 4;
148148
notSure = "maybe a string instead";
149149
notSure = false; // okay, definitely a boolean
@@ -153,7 +153,7 @@ The `any` type is a powerful way to work with existing JavaScript, allowing you
153153
You might expect `Object` to play a similar role, as it does in other languages.
154154
But variables of type `Object` only allow you to assign any value to them -- you can't call arbitrary methods on them, even ones that actually exist:
155155

156-
```TypeScript
156+
```ts
157157
var notSure: any = 4;
158158
notSure.ifItExists(); // okay, ifItExists might exist at runtime
159159
notSure.toFixed(); // okay, toFixed exists (but the compiler doesn't check)
@@ -164,7 +164,7 @@ prettySure.toFixed(); // Error: Property 'toFixed' doesn't exist on type 'Object
164164
The `any` type is also handy if you know some part of the type, but perhaps not all of it.
165165
For example, you may have an array but the array has a mix of different types:
166166

167-
```TypeScript
167+
```ts
168168
var list: any[] = [1, true, "free"];
169169

170170
list[1] = 100;
@@ -175,14 +175,14 @@ list[1] = 100;
175175
`void` is a little like the opposite of `any`: the absence of having any type at all.
176176
You may commonly see this as the return type of functions that do not return a value:
177177

178-
```TypeScript
178+
```ts
179179
function warnUser(): void {
180180
alert("This is my warning message");
181181
}
182182
```
183183

184184
Declaring variables of type `void` is not useful because you can only assign `undefined` or `null` to them:
185185

186-
```TypeScript
186+
```ts
187187
var unusable: void = undefined;
188188
```

pages/Classes.md

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ In TypeScript, we allow developers to use these techniques now, and compile them
88

99
Let's take a look at a simple class-based example:
1010

11-
```TypeScript
11+
```ts
1212
class Greeter {
1313
greeting: string;
1414
constructor(message: string) {
@@ -38,7 +38,7 @@ Of course, one of the most fundamental patterns in class-based programming is be
3838

3939
Let's take a look at an example:
4040

41-
```TypeScript
41+
```ts
4242
class Animal {
4343
name:string;
4444
constructor(theName: string) { this.name = theName; }
@@ -97,7 +97,7 @@ In TypeScript, each member is `public` by default.
9797
You may still mark a member `public` explicitly.
9898
We could have written the `Animal` class from the previous section in the following way:
9999

100-
```TypeScript
100+
```ts
101101
class Animal {
102102
public name: string;
103103
public constructor(theName: string) { this.name = theName; }
@@ -111,7 +111,7 @@ class Animal {
111111

112112
When a member is marked `private`, it cannot be accessed from outside of its containing class. For example:
113113

114-
```TypeScript
114+
```ts
115115
class Animal {
116116
private name: string;
117117
constructor(theName: string) { this.name = theName; }
@@ -129,7 +129,7 @@ The same applies to `protected` members.
129129

130130
Let's look at an example to better see how this plays out in practice:
131131

132-
```TypeScript
132+
```ts
133133
class Animal {
134134
private name: string;
135135
constructor(theName: string) { this.name = theName; }
@@ -163,7 +163,7 @@ Even though `Employee` also has a `private` member called `name`, it's not the a
163163

164164
The `protected` modifier acts much like the `private` modifier with the exception that members declared `protected` can also be accessed by instances of deriving classes. For example,
165165

166-
```TypeScript
166+
```ts
167167
class Person {
168168
protected name: string;
169169
constructor(name: string) { this.name = name; }
@@ -195,7 +195,7 @@ In our last example, we had to declare a private member `name` and a constructor
195195
This turns out to be a very common practice. *Parameter properties* let you create and initialize a member in one place.
196196
Here's a further revision of the previous `Animal` class using a parameter property:
197197

198-
```TypeScript
198+
```ts
199199
class Animal {
200200
constructor(private name: string) { }
201201
move(distanceInMeters: number) {
@@ -218,7 +218,7 @@ This gives you a way of having finer-grained control over how a member is access
218218
Let's convert a simple class to use `get` and `set`.
219219
First, let's start with an example without getters and setters.
220220

221-
```TypeScript
221+
```ts
222222
class Employee {
223223
fullName: string;
224224
}
@@ -236,7 +236,7 @@ In this version, we check to make sure the user has a secret passcode available
236236
We do this by replacing the direct access to `fullName` with a `set` that will check the passcode.
237237
We add a corresponding `get` to allow the previous example to continue to work seamlessly.
238238

239-
```TypeScript
239+
```ts
240240
var passcode = "secret passcode";
241241

242242
class Employee {
@@ -275,7 +275,7 @@ In this example, we use `static` on the origin, as it's a general value for all
275275
Each instance accesses this value through prepending the name of the class.
276276
Similarly to prepending `this.` in front of instance accesses, here we prepend `Grid.` in front of static accesses.
277277

278-
```TypeScript
278+
```ts
279279
class Grid {
280280
static origin = {x: 0, y: 0};
281281
calculateDistanceFromOrigin(point: {x: number; y: number;}) {
@@ -300,7 +300,7 @@ They may not be instantiated directly.
300300
Unlike an interface, an abstract class may contain implementation details for its members.
301301
The `abstract` keyword is used to define abstract classes as well as abstract methods within an abstract class.
302302

303-
```TypeScript
303+
```ts
304304
abstract class Animal {
305305
abstract makeSound(): void;
306306
move(): void {
@@ -314,7 +314,7 @@ Abstract methods share a similar syntax to interface methods.
314314
Both define the signature of a method without including a method body.
315315
However, abstract methods must include the `abstract` keyword and may optionally include access modifiers.
316316

317-
```TypeScript
317+
```ts
318318
abstract class Department {
319319

320320
constructor(public name: string) {
@@ -357,7 +357,7 @@ department.generateReports(); // error: method doesn't exist on declared abstrac
357357
When you declare a class in TypeScript, you are actually creating multiple declarations at the same time.
358358
The first is the type of the *instance* of the class.
359359

360-
```TypeScript
360+
```ts
361361
class Greeter {
362362
greeting: string;
363363
constructor(message: string) {
@@ -380,7 +380,7 @@ We're also creating another value that we call the *constructor function*.
380380
This is the function that is called when we `new` up instances of the class.
381381
To see what this looks like in practice, let's take a look at the JavaScript created by the above example:
382382

383-
```TypeScript
383+
```ts
384384
var Greeter = (function () {
385385
function Greeter(message) {
386386
this.greeting = message;
@@ -403,7 +403,7 @@ Another way to think of each class is that there is an *instance* side and a *st
403403

404404
Let's modify the example a bit to show this difference:
405405

406-
```TypeScript
406+
```ts
407407
class Greeter {
408408
static standardGreeting = "Hello, there";
409409
greeting: string;
@@ -444,7 +444,7 @@ We show this by using `new` on `greeterMaker`, creating new instances of `Greete
444444
As we said in the previous section, a class declaration creates two things: a type representing instances of the class and a constructor function.
445445
Because classes create types, you can use them in the same places you would be able to use interfaces.
446446

447-
```TypeScript
447+
```ts
448448
class Point {
449449
x: number;
450450
y: number;

pages/Declaration Merging.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Understanding what is created with each declaration will help you understand wha
3535
The simplest, and perhaps most common, type of declaration merging is interface merging.
3636
At the most basic level, the merge mechanically joins the members of both declarations into a single interface with the same name.
3737

38-
```TypeScript
38+
```ts
3939
interface Box {
4040
height: number;
4141
width: number;
@@ -56,7 +56,7 @@ Of note, too, is that in the case of interface `A` merging with later interface
5656

5757
That is, in the example:
5858

59-
```TypeScript
59+
```ts
6060
interface Document {
6161
createElement(tagName: any): Element;
6262
}
@@ -73,7 +73,7 @@ interface Document {
7373
The two interfaces will merge to create a single declaration.
7474
Notice that the elements of each group maintains the same order, just the groups themselves are merged with later overload sets coming first:
7575

76-
```TypeScript
76+
```ts
7777
interface Document {
7878
createElement(tagName: "div"): HTMLDivElement;
7979
createElement(tagName: "span"): HTMLSpanElement;
@@ -95,7 +95,7 @@ To merge the value, at each declaration site, if a namespace already exists with
9595

9696
The declaration merge of `Animals` in this example:
9797

98-
```TypeScript
98+
```ts
9999
namespace Animals {
100100
export class Zebra { }
101101
}
@@ -108,7 +108,7 @@ namespace Animals {
108108

109109
is equivalent to:
110110

111-
```TypeScript
111+
```ts
112112
namespace Animals {
113113
export interface Legged { numberOfLegs: number; }
114114

@@ -122,7 +122,7 @@ Non-exported members are only visible in the original (un-merged) namespace. Thi
122122

123123
We can see this more clearly in this example:
124124

125-
```TypeScript
125+
```ts
126126
namespace Animal {
127127
var haveMuscles = true;
128128

@@ -150,7 +150,7 @@ TypeScript uses this capability to model some of patterns in JavaScript as well
150150
The first namespace merge we'll cover is merging a namespace with a class.
151151
This gives the user a way of describing inner classes.
152152

153-
```TypeScript
153+
```ts
154154
class Album {
155155
label: Album.AlbumLabel;
156156
}
@@ -166,7 +166,7 @@ You can also use namespaces to add more static members to an existing class.
166166
In addition to the pattern of inner classes, you may also be familiar with JavaScript practice of creating a function and then extending the function further by adding properties onto the function.
167167
TypeScript uses declaration merging to build up definitions like this in a type-safe way.
168168

169-
```TypeScript
169+
```ts
170170
function buildLabel(name: string): string {
171171
return buildLabel.prefix + name + buildLabel.suffix;
172172
}
@@ -181,7 +181,7 @@ alert(buildLabel("Sam Smith"));
181181

182182
Similarly, namespaces can be used to extend enums with static members:
183183

184-
```TypeScript
184+
```ts
185185
enum Color {
186186
red = 1,
187187
green = 2,

0 commit comments

Comments
 (0)