forked from DefinitelyTyped/DefinitelyTyped
-
Notifications
You must be signed in to change notification settings - Fork 0
/
next.tsx
173 lines (140 loc) · 4.68 KB
/
next.tsx
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/// <reference types="../next"/>
import React = require('react');
const { useSyncExternalStore } = React;
// We need these Window interfaces to compile
interface Window {
location: {
href: string;
pathname: string;
};
addEventListener(type: string, callback: () => void): void;
removeEventListener(type: string, callback: () => void): void;
}
const noop = () => {};
const window: Window = { location: { href: '', pathname: '' }, addEventListener: noop, removeEventListener: noop };
const locationSource = React.unstable_createMutableSource(window, () => window.location.href);
const getSnapshot = (window: Window) => window.location.pathname;
const subscribe: React.MutableSourceSubscribe<Window> = (window, callback) => {
window.addEventListener("popstate", callback);
return () => window.removeEventListener("popstate", callback);
};
function useExperimentalHooks() {
const [toggle, setToggle] = React.useState(false);
React.useTransition();
React.useTransition({});
const [done, startTransition] = React.useTransition({ timeoutMs: 1000 });
// $ExpectType boolean
done;
// $ExpectType boolean
const deferredToggle = React.useDeferredValue(toggle);
const [func] = React.useState(() => () => 0);
// $ExpectType () => number
func;
// $ExpectType () => number
const deferredFunc = React.useDeferredValue(func);
class Constructor {}
// $ExpectType typeof Constructor
const deferredConstructor = React.useDeferredValue(Constructor);
// $ExpectType () => string
const deferredConstructible = React.useDeferredValue(Constructible);
// $ExpectType string
const pathName = React.unstable_useMutableSource(locationSource, getSnapshot, subscribe);
React.useInsertionEffect(() => {});
React.useInsertionEffect(() => {}, []);
React.useInsertionEffect(() => {
return () => {};
}, [toggle]);
return () => {
startTransition(() => {
setToggle(toggle => !toggle);
});
// The function must be synchronous, even if it can start an asynchronous update
// it's no different from an useEffect callback in this respect
// $ExpectError
startTransition(async () => {});
// Unlike Effect callbacks, though, there is no possible destructor to return
// $ExpectError
startTransition(() => () => {});
};
function Constructible() {
return '';
}
}
function startTransitionTest() {
function transitionToPage(page: string) {}
React.startTransition(() => {
transitionToPage('/');
});
// $ExpectError
React.startTransition(async () => {});
}
function suspenseTest() {
function DisplayData() {
return null;
}
function FlameChart() {
return (
<React.Suspense fallback="computing..." unstable_expectedLoadTime={2000}>
<DisplayData />
</React.Suspense>
);
}
}
function Dialog() {
const id = React.useId();
const nameId = `${id}-name`;
const descriptionId = `${id}-description`;
return (
<div role="dialog" aria-labelledby={nameId} aria-describedby={descriptionId}>
<h2 id={nameId}>Name</h2>
<p id={descriptionId}>Description</p>
</div>
);
}
function SuspenseTest() {
// TODO(react18): Should not error.
// `fallback` is optional in React 18
// $ExpectError
<React.Suspense></React.Suspense>;
// TODO(react18): Should not error.
// `fallback` is optional in React 18.
// $ExpectError
<React.Suspense fallback={undefined}></React.Suspense>;
// Workaround.
// TODO(react18): Remove.
<React.Suspense fallback={undefined!}></React.Suspense>;
}
// keep in sync with `use-sync-external-store-tests.ts`
interface Store<State> {
getState(): State;
getServerState(): State;
subscribe(onStoreChange: () => void): () => void;
}
declare const numberStore: Store<number>;
function useVersion(): number {
return useSyncExternalStore(numberStore.subscribe, numberStore.getState);
}
function useStoreWrong() {
useSyncExternalStore(
// no unsubscribe returned
// $ExpectError
() => {
return null;
},
() => 1,
);
// `string` is not assignable to `number`
// $ExpectError
const version: number = useSyncExternalStore(
() => () => {},
() => '1',
);
}
declare const objectStore: Store<{ version: { major: number; minor: number }; users: string[] }>;
function useUsers(): string[] {
return useSyncExternalStore(
objectStore.subscribe,
() => objectStore.getState().users,
() => objectStore.getServerState().users,
);
}