Skip to content

Commit b016d2f

Browse files
committed
Add type overloads for the createSuperContext function to signify that it returns a factory function that doesn't accept any props when creating a super context that don't accept props. Make the contexts prop for the SuperContext component no longer accept factory functions that accept props. This makes it so that TypeScript will show an error if you forget to pass props to a super context that requires them when providing the context.
1 parent 93a3f73 commit b016d2f

File tree

4 files changed

+37
-4
lines changed

4 files changed

+37
-4
lines changed

CHANGELOG.md

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,36 @@
11
# Changelog
22

3-
## 0.2.1 (_27 February 2021_)
3+
4+
## 0.2.2
5+
_9 April 2021_
6+
7+
* TypeScript improvements: When providing a super context that accepts props, you will now get a type error if you don't provide any props
8+
9+
Example:
10+
```tsx
11+
// CounterContext.ts
12+
const [counterContext, useCounter] = createSuperContext(({ initial }: CounterContextProps) => {
13+
...
14+
});
15+
16+
// App.tsx
17+
// This is now a type error:
18+
const App = () => <SuperContext contexts={[counterContext]}></SuperContext>
19+
20+
// Correct usage (no errors):
21+
const App = () => <SuperContext contexts={[counterContext({ initial: 10 })]}></SuperContext>
22+
```
23+
24+
## 0.2.1
25+
_27 February 2021_
26+
427
* Mark the `interceptors` feature as deprecated and link to wiki explaining why in docs and console warnings.
528
* Remove `interceptors` examples.
629
* Remove experimental warning from readme since API is now unlikely to change before version 1.0.0.
730

8-
## 0.2.0 (_21 January 2021_)
31+
## 0.2.0
32+
_21 January 2021_
33+
934
* New `testValue` option for contexts. This is the value returned by the super context hook in a test environment.
1035
* New `testEnvironment` option for context. Can be used to override the `NODE_ENV === "test"` check to determine if running in a test environment.
1136

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"boilerplate"
1010
],
1111
"license": "MIT",
12-
"version": "0.2.1",
12+
"version": "0.2.2",
1313
"repository": {
1414
"url": "https://github.com/goransh/react-super-context"
1515
},

src/CreateSuperContext.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ export interface CreateSuperContextOptions<T> {
3333
}
3434

3535
// could maybe benefit from partial type argument inference: https://github.com/microsoft/TypeScript/issues/26242
36+
export function createSuperContext<T>(
37+
factory: () => T,
38+
options?: Partial<CreateSuperContextOptions<T>>
39+
): [() => SuperContextDefinition<any, T>, () => T];
40+
export function createSuperContext<T, P = any>(
41+
factory: (props: P) => T,
42+
options?: Partial<CreateSuperContextOptions<T>>
43+
): [(props: P) => SuperContextDefinition<P, T>, () => T];
3644
export function createSuperContext<T, P = any>(
3745
factory: (props: P) => T,
3846
options: Partial<CreateSuperContextOptions<T>> = {}

src/SuperContext.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export type SuperContextProps = PropsWithChildren<{
55
/**
66
* Contexts provided by the SuperContext.
77
*/
8-
contexts: (SuperContextDefinition | ((props?: any) => SuperContextDefinition))[];
8+
contexts: (SuperContextDefinition | (() => SuperContextDefinition))[];
99
/**
1010
* Options to apply to all contexts provided by the SuperContext. Will be overwritten by any
1111
* context specific options.

0 commit comments

Comments
 (0)