Skip to content

Commit 33c0f10

Browse files
authored
Merge pull request #5 from total-typescript/matt/5.0
Matt/5.0
2 parents f5fc11d + 2ad098f commit 33c0f10

12 files changed

+173
-96
lines changed

notes/FUTURE.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ With a non-generic external library
6767
### 6. Identity Functions
6868

6969
✅ No generics on objects!
70-
✅ F.Narrow vs as const
7170
✅ F.NoInfer
7271
✅ Lodash (MAYBE REVISIT AND ADD COMPLEXITY)
7372

package-lock.json

Lines changed: 30 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"@types/node": "^18.6.5",
99
"chokidar": "^3.5.3",
1010
"cross-fetch": "^3.1.5",
11-
"typescript": "^4.8.3",
11+
"prettier": "^2.8.7",
12+
"typescript": "^5.0.2",
1213
"vitest": "^0.21.1"
1314
},
1415
"scripts": {
@@ -100,4 +101,4 @@
100101
"ts-toolbelt": "^9.6.0",
101102
"zod": "^3.19.1"
102103
}
103-
}
104+
}

src/06-identity-functions/27-as-const-alternative.problem.ts

Lines changed: 0 additions & 46 deletions
This file was deleted.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { Equal, Expect } from "../helpers/type-utils";
2+
3+
export const asConst = <T>(t: T) => t;
4+
5+
const fruits = asConst([
6+
{
7+
name: "apple",
8+
price: 1,
9+
},
10+
{
11+
name: "banana",
12+
price: 2,
13+
},
14+
]);
15+
16+
type tests = [
17+
Expect<
18+
Equal<
19+
typeof fruits,
20+
readonly [
21+
{
22+
readonly name: "apple";
23+
readonly price: 1;
24+
},
25+
{
26+
readonly name: "banana";
27+
readonly price: 2;
28+
}
29+
]
30+
>
31+
>
32+
];
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { Equal, Expect } from "../helpers/type-utils";
2+
3+
export const asConst = <const T>(t: T) => t;
4+
5+
const fruits = asConst([
6+
{
7+
name: "apple",
8+
price: 1,
9+
},
10+
{
11+
name: "banana",
12+
price: 2,
13+
},
14+
]);
15+
16+
type tests = [
17+
Expect<
18+
Equal<
19+
typeof fruits,
20+
readonly [
21+
{
22+
readonly name: "apple";
23+
readonly price: 1;
24+
},
25+
{
26+
readonly name: "banana";
27+
readonly price: 2;
28+
}
29+
]
30+
>
31+
>
32+
];

src/06-identity-functions/27-as-const-alternative.solution.ts renamed to src/06-identity-functions/27-const-annotations.solution.2.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
1-
import { F } from "ts-toolbelt";
21
import { Equal, Expect } from "../helpers/type-utils";
2+
import { F } from "ts-toolbelt";
33

44
export const asConst = <T>(t: F.Narrow<T>) => t;
55

6-
/**
7-
* Now, fruits is typed as:
8-
* [{ name: "apple"; price: 1 }, { name: "banana"; price: 2 }]
9-
*
10-
* Try changing the argument to asConst to see how it affects
11-
* the type.
12-
*/
136
const fruits = asConst([
147
{
158
name: "apple",
@@ -33,8 +26,8 @@ type tests = [
3326
{
3427
name: "banana";
3528
price: 2;
36-
},
29+
}
3730
]
3831
>
39-
>,
32+
>
4033
];

src/06-identity-functions/28-constraints-with-f.narrow.problem.ts renamed to src/06-identity-functions/28-constraints-with-const-annotations.problem.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
1-
import { F } from "ts-toolbelt";
21
import { it } from "vitest";
32
import { Equal, Expect } from "../helpers/type-utils";
43

5-
/**
6-
* We know that asConst works, but we now also want to be
7-
* able to narrow the type to only allow an array
8-
* of fruits.
9-
*/
104
export const narrowFruits = <TFruits>(t: TFruits) => t;
115

126
const fruits = narrowFruits([
@@ -24,14 +18,14 @@ type tests = [
2418
Expect<
2519
Equal<
2620
typeof fruits,
27-
[
21+
readonly [
2822
{
29-
name: "apple";
30-
price: 1;
23+
readonly name: "apple";
24+
readonly price: 1;
3125
},
3226
{
33-
name: "banana";
34-
price: 2;
27+
readonly name: "banana";
28+
readonly price: 2;
3529
}
3630
]
3731
>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { it } from "vitest";
2+
import { Equal, Expect } from "../helpers/type-utils";
3+
4+
type FruitsConstraint = readonly {
5+
name: string;
6+
price: number;
7+
}[];
8+
9+
export const narrowFruits = <const TFruits extends FruitsConstraint>(
10+
t: TFruits
11+
) => t;
12+
13+
const fruits = narrowFruits([
14+
{
15+
name: "apple",
16+
price: 1,
17+
},
18+
{
19+
name: "banana",
20+
price: 2,
21+
},
22+
]);
23+
24+
type tests = [
25+
Expect<
26+
Equal<
27+
typeof fruits,
28+
readonly [
29+
{
30+
readonly name: "apple";
31+
readonly price: 1;
32+
},
33+
{
34+
readonly name: "banana";
35+
readonly price: 2;
36+
}
37+
]
38+
>
39+
>
40+
];
41+
42+
it("Should ONLY let you pass an array of fruits", () => {
43+
const notAllowed = narrowFruits([
44+
// @ts-expect-error
45+
"not allowed",
46+
]);
47+
});

src/06-identity-functions/28-constraints-with-f.narrow.solution.ts renamed to src/06-identity-functions/28-constraints-with-const-annotations.solution.2.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
import { F } from "ts-toolbelt";
21
import { it } from "vitest";
32
import { Equal, Expect } from "../helpers/type-utils";
43

5-
export const narrowFruits = <TFruits extends { name: string; price: number }[]>(
6-
t: F.Narrow<TFruits>,
4+
export const narrowFruits = <
5+
const TFruits extends ReadonlyArray<{
6+
name: string;
7+
price: number;
8+
}>
9+
>(
10+
t: TFruits
711
) => t;
812

913
const fruits = narrowFruits([
@@ -21,18 +25,18 @@ type tests = [
2125
Expect<
2226
Equal<
2327
typeof fruits,
24-
[
28+
readonly [
2529
{
26-
name: "apple";
27-
price: 1;
30+
readonly name: "apple";
31+
readonly price: 1;
2832
},
2933
{
30-
name: "banana";
31-
price: 2;
32-
},
34+
readonly name: "banana";
35+
readonly price: 2;
36+
}
3337
]
3438
>
35-
>,
39+
>
3640
];
3741

3842
it("Should ONLY let you pass an array of fruits", () => {

0 commit comments

Comments
 (0)