Repository navigation
Expand file tree
/
Copy pathType Widening and Narrowing.ts
More file actions
67 lines (51 loc) · 2.16 KB
/
Copy pathType Widening and Narrowing.ts
File metadata and controls
67 lines (51 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// It might be easiest to start of the discussion of
// widening and narrowing with an example:
const welcomeString = "Hello There";
let replyString = "Hey";
// Aside from the text differences of the strings, welcomeString
// is a const (which means the value will never change)
// and replyString is a let (which means it can change).
// If you hover over both variables, you get very different
// type information from TypeScript:
//
// const welcomeString: "Hello There"
//
// let replyString: string
// TypeScript has inferred the type of welcomeString to be
// the literal string "Hello There", whereas replyString
// is general string.
// This is because a let needs to have a wider type, you
// could set replyString to be any other string - which means
// it has a wider set of possibilities.
replyString = "Hi :wave:";
// If replyString had the string literal type "Hey" - then
// you could never change the value because it could only
// change to "Hey" again.
// Widening and Narrowing types is about expanding and reducing
// the possibilities which a type could represent.
// An example of type narrowing is working with unions, the
// example on code flow analysis is almost entirely based on
// narrowing: example:code-flow
// Type narrowing is what powers the strict mode of TypeScript
// via the nullability checks. With strict mode turned off,
// markers for nullability like undefined and null are ignored
// in a union.
declare const quantumString: string | undefined;
// This will fail in strict mode only
quantumString.length;
// In strict mode the onus is on the code author to ensure
// that the type has been narrowed to the non-null type.
// Usually this is as simple as an if check:
if (quantumString) {
quantumString.length;
}
// In strict mode the type quantumString has two representations.
// Inside the if, the type was narrowed to just string.
// You can see more examples of narrowing in:
//
// example:union-and-intersection-types
// example:discriminate-types
// And even more resources on the web:
//
// https://mariusschulz.com/blog/literal-type-widening-in-typescript
// https://sandersn.github.io/manual/Widening-and-Narrowing-in-Typescript.html