-
-
Notifications
You must be signed in to change notification settings - Fork 852
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
Add TypeScript Mapped Draft Type #161
Conversation
I just pushed a commit to make the interface State {
readonly foo: number;
readonly baz: {
readonly x: number;
readonly y: number;
};
} I'm still trying to figure out how to make arrays work properly. |
Looks like everything is working now. All of the following compiles and works as expected: import produce from 'immer';
interface State {
readonly num: number;
readonly foo?: string;
bar: string;
readonly baz: {
readonly x: number;
readonly y: number;
};
readonly arr: ReadonlyArray<{ readonly value: string }>;
readonly arr2: { readonly value: string }[];
}
const state: State = {
num: 0,
bar: 'foo',
baz: {
x: 1,
y: 2,
},
arr: [{ value: 'asdf' }],
arr2: [{ value: 'asdf' }],
}
const newState = produce<State>(draft => {
draft.num++;
draft.foo = 'bar';
draft.bar = 'foo';
draft.baz.x++;
draft.baz.y++;
draft.arr[0].value = 'foo';
draft.arr.push({ value: 'asf' });
draft.arr2[0].value = 'foo';
draft.arr2.push({ value: 'asf' });
})(state); |
This would be really useful to us, thank you very much @knpwrs |
@mweststrate is there any interest from the project owners to add these typings? |
Yes; but didn't get to reviewing yet 😊
Op za 23 jun. 2018 19:42 schreef Ken Powers <[email protected]>:
… @mweststrate <https://github.com/mweststrate> is there any interest from
the project owners to add these typings?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#161 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ABvGhFRsW1xcoEuuMj_GEeYBBdiVUNHAks5t_n4igaJpZM4UhG-U>
.
|
Hello @mweststrate |
Reviewed! No comments, looks awesome :). Will release next week |
These changes enable the following:
This helps TypeScript users ensure immutability of their state interfaces. I had to upgrade TypeScript for the
index.d.ts
file to parse properly. This feature was merged into TypeScript back in February: microsoft/TypeScript#21919It also appears that theflow/ts.ts
file doesn't run and never did. I tested this externally and it works, but I'd like to see a more robust test suite in this repo.EDIT: I've added some TypeScript tests in Jest.
EDIT: Fixes #97