Skip to content

Commit

Permalink
fix contravariance of writeable state:
Browse files Browse the repository at this point in the history
  • Loading branch information
csantos42 committed May 29, 2020
1 parent 4128d12 commit f6ddc8d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
9 changes: 5 additions & 4 deletions types/recoil/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,15 +158,16 @@ export type Loadable<T> =

// recoilValue.d.ts
export class AbstractRecoilValue<T> {
tag: 'Writeable';
valTag: T;
__tag: [T];
__cTag: (t: T) => void; // for contravariance

key: NodeKey;
constructor(newKey: NodeKey);
}

export class AbstractRecoilValueReadonly<T> {
tag: 'Readonly';
valTag: T;
__tag: [T];

key: NodeKey;
constructor(newKey: NodeKey);
}
Expand Down
17 changes: 17 additions & 0 deletions types/recoil/recoil-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
useResetRecoilState,
useRecoilCallback,
isRecoilValue,
RecoilState,
} from 'recoil';
// import { atomFamily } from 'recoil/utils';

Expand Down Expand Up @@ -73,6 +74,22 @@ RecoilRoot({
});

// Hooks
const roAtom: RecoilValueReadOnly<string> = {} as any;
const waAtom: RecoilState<string> = {} as any;
const nsAtom: RecoilState<number | string> = {} as any; // number or string

useRecoilValue(roAtom);
useRecoilValue(waAtom);

useRecoilState(roAtom); // $ExpectError
useRecoilState(waAtom);

useRecoilState<number>(waAtom); // $ExpectError
useRecoilState<number | string>(waAtom); // $ExpectError
useRecoilValue<number>(waAtom); // $ExpectError
useRecoilValue<number | string>(waAtom);
useRecoilValue<number>(nsAtom); // $ExpectError

useRecoilValue(myAtom);
useRecoilValue(mySelector1);
useRecoilValue(readOnlySelector);
Expand Down

0 comments on commit f6ddc8d

Please sign in to comment.