Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typing fixes for 0.7 #1690

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion eslint-rules/no-fb-only.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ module.exports = {
if (match) {
context.report({
message:
`Usage of "${descriptor}". Please consider depenedncy injecting this condition ` +
`Usage of "${descriptor}". Please consider dependency injecting this condition ` +
`instead. See "${__filename}" for more details`,
loc: {
start: {line: lineNumber + 1, column: 0},
Expand Down
9 changes: 7 additions & 2 deletions packages/recoil/recoil_values/Recoil_atomFamily.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,12 @@ export type AtomFamilyOptions<T, P: Parameter> =
| Loadable<T>
| WrappedValue<T>
| T
| (P => T | RecoilValue<T> | Promise<T>),
| (P =>
| T
| RecoilValue<T>
| Promise<T>
| Loadable<T>
| WrappedValue<T>),
}>
| AtomFamilyOptionsWithoutDefault<T, P>;

Expand Down Expand Up @@ -118,7 +123,7 @@ function atomFamily<T, P: Parameter>(
| Loadable<T>
| WrappedValue<T>
| T
| (P => T | RecoilValue<T> | Promise<T>) =
| (P => T | RecoilValue<T> | Promise<T> | Loadable<T> | WrappedValue<T>) =
'default' in options
? // $FlowIssue[prop-missing] No way to refine in Flow that property is not defined
// $FlowIssue[incompatible-type] No way to refine in Flow that property is not defined
Expand Down
32 changes: 32 additions & 0 deletions packages/recoil/recoil_values/__tests__/Recoil_atomFamily-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ let store: Store,
atom,
atomFamily,
selectorFamily,
RecoilLoadable,
pAtom;

const testRecoil = getRecoilTestFn(() => {
Expand Down Expand Up @@ -72,6 +73,7 @@ const testRecoil = getRecoilTestFn(() => {
atom = require('../Recoil_atom');
atomFamily = require('../Recoil_atomFamily');
selectorFamily = require('../Recoil_selectorFamily');
({RecoilLoadable} = require('../../adt/Recoil_Loadable'));

store = makeStore();

Expand Down Expand Up @@ -151,6 +153,36 @@ describe('Default', () => {
expect(get(paramDefaultAtom({num: 1}))).toBe(3);
expect(get(paramDefaultAtom({num: 2}))).toBe(2);
});

testRecoil('Parameterized async default', async () => {
const paramDefaultAtom = atomFamily({
key: 'parameterized async default',
default: ({num}) =>
num === 1 ? Promise.reject(num) : Promise.resolve(num),
});
await expect(get(paramDefaultAtom({num: 1}))).rejects.toBe(1);
await expect(get(paramDefaultAtom({num: 2}))).resolves.toBe(2);
set(paramDefaultAtom({num: 1}), 3);
expect(get(paramDefaultAtom({num: 1}))).toBe(3);
expect(get(paramDefaultAtom({num: 2}))).toBe(2);
});

testRecoil('Parameterized loadable default', async () => {
const paramDefaultAtom = atomFamily({
key: 'parameterized loadable default',
default: ({num}) =>
num === 1 ? RecoilLoadable.error(num) : RecoilLoadable.of(num),
});
expect(getLoadable(paramDefaultAtom({num: 1})).state).toBe('hasError');
expect(getLoadable(paramDefaultAtom({num: 1})).contents).toBe(1);
expect(getLoadable(paramDefaultAtom({num: 2})).state).toBe('hasValue');
expect(getLoadable(paramDefaultAtom({num: 2})).contents).toBe(2);
set(paramDefaultAtom({num: 1}), 3);
expect(getLoadable(paramDefaultAtom({num: 1})).state).toBe('hasValue');
expect(getLoadable(paramDefaultAtom({num: 1})).contents).toBe(3);
expect(getLoadable(paramDefaultAtom({num: 2})).state).toBe('hasValue');
expect(getLoadable(paramDefaultAtom({num: 2})).contents).toBe(2);
});
});

testRecoil('Works with date as parameter', () => {
Expand Down
4 changes: 2 additions & 2 deletions typescript/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ interface AtomFamilyOptionsWithoutDefault<T, P extends SerializableParam> {
| Loadable<T>
| WrappedValue<T>
| T
| ((param: P) => T | RecoilValue<T> | Promise<T>);
| ((param: P) => T | RecoilValue<T> | Promise<T> | Loadable<T> | WrappedValue<T>);
}
export type AtomFamilyOptions<T, P extends SerializableParam> =
| AtomFamilyOptionsWithDefault<T, P>
Expand Down Expand Up @@ -440,7 +440,7 @@ interface AtomFamilyOptionsWithoutDefault<T, P extends SerializableParam> {
get: (param: P) => (opts: {
get: GetRecoilValue,
getCallback: GetCallback,
}) => Promise<T> | RecoilValue<T> | T;
}) => Promise<T> | Loadable<T> | WrappedValue<T> | RecoilValue<T> | T;
set: (
param: P,
) => (
Expand Down
7 changes: 7 additions & 0 deletions typescript/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,13 @@ isRecoilValue(mySelector1);
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const myAtomFamilyWithoutDefault: (number: number) => RecoilState<number> =
atomFamily<number, number>({key: 'MyAtomFamilyWithoutDefault'});

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const myAsyncAtomFamily: (number: number) => RecoilState<number> =
atomFamily<number, number>({
key: 'MyAsyncAtomFamily',
default: (param: number) => Promise.resolve(param),
});
}

/**
Expand Down