Skip to content

Commit 4ac1144

Browse files
committed
Added explainers
1 parent c4aaa36 commit 4ac1144

File tree

5 files changed

+109
-0
lines changed

5 files changed

+109
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Explain fetch-wrapper.ts in cal.com repo
2+
3+
export {};
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// When returning the value only, it infers
2+
// the literal type
3+
const returnsValueOnly = <T>(t: T) => {
4+
return t;
5+
};
6+
7+
const result = returnsValueOnly("a");
8+
// ^?
9+
10+
// When returning an object or array, it doesn't infer the
11+
// literal type
12+
const returnsValueInAnObject = <T1>(t: T1) => {
13+
return {
14+
t,
15+
};
16+
};
17+
18+
const result2 = returnsValueInAnObject("abc");
19+
// ^?
20+
21+
// With a constraint, it narrows it to its literal
22+
const returnsValueInAnObjectWithConstraint = <T1 extends string>(t: T1) => {
23+
return {
24+
t,
25+
};
26+
};
27+
28+
const result3 = returnsValueInAnObjectWithConstraint("abc");
29+
// ^?
30+
31+
export {};
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// When returning the value only, it infers
2+
// the literal type
3+
const acceptsValueOnly = <T>(t: T) => {
4+
return t;
5+
};
6+
7+
const result = acceptsValueOnly("a");
8+
// ^?
9+
10+
const acceptsValueInAnObject = <T>(obj: { input: T }) => {
11+
return obj.input;
12+
};
13+
14+
const result2 = acceptsValueInAnObject({ input: "abc" });
15+
// ^?
16+
17+
const result2WithAsConst = acceptsValueInAnObject({ input: "abc" } as const);
18+
// ^?
19+
20+
const acceptsValueInAnObjectFieldWithConstraint = <T extends string>(obj: {
21+
input: T;
22+
}) => {
23+
return obj.input;
24+
};
25+
26+
const result3 = acceptsValueInAnObjectFieldWithConstraint({ input: "abc" });
27+
// ^?
28+
29+
const acceptsValueWithObjectConstraint = <
30+
T extends {
31+
input: string;
32+
},
33+
>(
34+
obj: T,
35+
) => {
36+
return obj.input;
37+
};
38+
39+
const result4 = acceptsValueWithObjectConstraint({ input: "abc" });
40+
41+
const result4WithAsConst = acceptsValueWithObjectConstraint({
42+
input: "abc",
43+
} as const);
44+
45+
export {};
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { expect, it } from "vitest";
2+
import { Equal, Expect } from "../helpers/type-utils";
3+
4+
type GreetingResult<TGreeting> = TGreeting extends "hello"
5+
? "goodbye"
6+
: "hello";
7+
8+
function youSayGoodbyeISayHello<TGreeting extends "hello" | "goodbye">(
9+
greeting: TGreeting,
10+
) {
11+
return (
12+
greeting === "goodbye" ? "hello" : "goodbye"
13+
) as GreetingResult<TGreeting>;
14+
}
15+
16+
it("Should return goodbye when hello is passed in", () => {
17+
const result = youSayGoodbyeISayHello("hello");
18+
19+
type test = [Expect<Equal<typeof result, "goodbye">>];
20+
21+
expect(result).toEqual("goodbye");
22+
});
23+
24+
it("Should return hello when goodbye is passed in", () => {
25+
const result = youSayGoodbyeISayHello("goodbye");
26+
27+
type test = [Expect<Equal<typeof result, "hello">>];
28+
29+
expect(result).toEqual("hello");
30+
});

0 commit comments

Comments
 (0)