Skip to content

Commit bbf451a

Browse files
committed
Updated 28
1 parent 9176bdc commit bbf451a

File tree

2 files changed

+56
-4
lines changed

2 files changed

+56
-4
lines changed
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-const-annotations.solution.ts renamed to src/06-identity-functions/28-constraints-with-const-annotations.solution.2.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
import { it } from "vitest";
22
import { Equal, Expect } from "../helpers/type-utils";
33

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

813
const fruits = narrowFruits([
@@ -28,10 +33,10 @@ type tests = [
2833
{
2934
readonly name: "banana";
3035
readonly price: 2;
31-
},
36+
}
3237
]
3338
>
34-
>,
39+
>
3540
];
3641

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

0 commit comments

Comments
 (0)