Skip to content

Commit 0daaee9

Browse files
committed
Updates, completing incomplete sentences
1 parent 10431b4 commit 0daaee9

File tree

7 files changed

+473
-222
lines changed

7 files changed

+473
-222
lines changed

README.md

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,40 @@ TypeScript makes some normally complex tasks very easy, but it can make some see
44

55
## Some things that were hard: now easy!
66

7-
Change an API? Change the type and you'll see everywhere you need to update. Need to rename something used in 500 places? Hit F2 and rename. Anything requiring fle
7+
Change an API? Change the type and you'll see everywhere you need to update. Need to rename something used in 500 places? Hit F2 and rename.
88

99
## Some things that were easy: now hard.
1010

11-
TypeScript actively discourages highly-dynamic code, redefining variables to mean different things, and anything other than simple (property) strings for APIs. What do these look like?
11+
TypeScript actively discourages highly-dynamic code, redefining variables at will, and APIs that put a lot of meaning into strings. What do these look like?
1212

1313
```tsx
14-
// Dynamic code:
14+
const sizes = {
15+
"size-sm": 1,
16+
"size-md": 2,
17+
"size-lg": 3,
18+
};
19+
20+
const getSize = (size: "sm" | "md" | "lg") => {
21+
return sizes[`size-${size}`];
22+
};
1523
```
1624

25+
This site is all about letting you know about these limitations _without_ all the type-system jargon you'll normally see.
26+
27+
# Diagnostic Codes
28+
29+
TypeScript has a code for every error, such as `ts(2741)`. While the error message may look very different each time, the underlying problem is the same.
30+
31+
If you're hitting a problem, search for that code within this page.
32+
1733
# For Site / Application Developers
1834

1935
## I'm trying to start with an empty object and add properties one by one, and TypeScript gives me errors no matter what I do. I've given up and `// @ts-ignore`d them.
2036

2137
When you create an empty object, there's just no good way for TS to ensure that all properties have been added _after_ the object has already been specified. Don't worry, though - there are lots of ways to accomplish this, and one of them is very concise as well! See: [Build Up Object](examples/build-up-object.ts).
2238

39+
Diagnostic codes: `ts(2339)`, `ts(2741)`
40+
2341
## What's this `never` type?
2442

2543
This is covered in the new handbook under the [never type](https://microsoft.github.io/TypeScript-New-Handbook/chapters/more-on-functions/#never).

examples/limitations.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export {};
22

33
// go over APIs which can't be modeled at all
44
// example: camelize
5+
// issue: https://github.com/microsoft/TypeScript/issues/12754
56
() => {
67
// from: https://stackoverflow.com/questions/2970525/converting-any-string-into-camel-case
78
const camelize = (str: string) => {

examples/never.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,18 @@ function fail(msg: string): never {
1919
}
2020

2121
// The other time you'll see `never` is when creating a new empty array, when
22-
// noImplicityAny is set to `false`. It is extremely unlikely you will see this,
23-
// but the type defaults to `never[]` to prevent it from being inferred as `any[]`.
22+
// noImplicityAny is set to `false`. It is unlikely you will see this, but the
23+
// type defaults to `never[]` to prevent it from being inferred as `any[]`.
2424
// Simple fix: give it a type, or set `noImplicitAny` to true.
2525
// https://github.com/microsoft/TypeScript/pull/8944
2626

27-
const emptyArray = []; // never[]
28-
emptyArray.push(1); // nope!
29-
const emptyNumberArray: number[] = [];
27+
// Sometimes `never[]` is really a symptom of another error. Here's a case where
28+
// an object is
29+
30+
type Resolver = {
31+
location: { city: string; state: string };
32+
};
33+
34+
const resolver: Resolver = {
35+
location: [],
36+
};

examples/safe-property-access.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,6 @@ const doStuffWithUnions = (data?: UnreliableUnionData) => {
9191
oc(structure as StructureOne).deepMayExist.nullableNumber(0);
9292
};
9393

94-
// Destructuring with an empty object by default can result in confusing errors:
9594
const doStuffWithDefaults = (data: UnreliableData) => {
96-
// "Initializer provides no value for this binding element and the binding element has no default value.ts(2525)"
97-
const { nullableString }: UnreliableData["mayExist"] = data.mayExist || {};
98-
// This will be fixed when TypeScript 3.6 is released:
99-
// https://github.com/microsoft/TypeScript/pull/31711
95+
const { deepMayExist } = data.mayExist || {};
10096
};

examples/type-widening.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export {};
66

77
// tl;dr rules for avoiding this:
88
// 1. never reassign parameters. https://eslint.org/docs/rules/no-param-reassign
9-
// 2.
9+
// 2. use `const` unless you need `let` (enable the prefer-const eslint rule)
1010

1111
const maybeAsync = (callback: () => void) => {
1212
callback();

package.json

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,18 @@
1010
"author": "",
1111
"license": "ISC",
1212
"dependencies": {
13-
"@types/lodash": "^4.14.135",
14-
"@types/react": "^16.8.13",
15-
"@typescript-eslint/eslint-plugin": "^1.6.0",
16-
"@typescript-eslint/parser": "^1.6.0",
17-
"eslint": "^5.16.0",
18-
"eslint-plugin-jsx-a11y": "^6.2.1",
19-
"eslint-plugin-react": "^7.12.4",
20-
"eslint-plugin-react-hooks": "^1.6.0",
21-
"lodash": "^4.17.13",
22-
"react": "^16.8.6",
23-
"redux": "^4.0.1",
13+
"@types/lodash": "^4.14.149",
14+
"@types/react": "^16.9.16",
15+
"@typescript-eslint/eslint-plugin": "^2.11.0",
16+
"@typescript-eslint/parser": "^2.11.0",
17+
"eslint": "^6.7.2",
18+
"eslint-plugin-jsx-a11y": "^6.2.3",
19+
"eslint-plugin-react": "^7.17.0",
20+
"eslint-plugin-react-hooks": "^2.3.0",
21+
"lodash": "^4.17.15",
22+
"react": "^16.12.0",
23+
"redux": "^4.0.4",
2424
"redux-thunk": "^2.3.0",
25-
"ts-optchain": "^0.1.4",
26-
"typescript": "^3.4.2"
25+
"typescript": "^3.7.3"
2726
}
2827
}

0 commit comments

Comments
 (0)