-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Allow using 'as const' for creating tuples for map.replace #3344
Allow using 'as const' for creating tuples for map.replace #3344
Conversation
🦋 Changeset detectedLatest commit: d92353b The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Thank you for the PR. If you explicitly specify the type, this TS error will also disappear: const dispose = mobx.reaction(
() => {
const values: Array<[string, number]> = Array.from(srcValues, (value, index) => [value, index])
return values;
},
(entries) => map.replace(entries)
) Considering that there is no This example compiles successfully: const srcValues: string[] = [];
const map = new Map<string, number>(
Array.from(srcValues, (value, index) => [value, index] as const);
); This one doesn't const srcValues = mobx.observable.array<string>()
const map = mobx.observable.map<string, number>(
Array.from(srcValues, (value, index) => [value, index] as const)
) That is because TS Map constructor has the following definition: interface MapConstructor {
new(): Map<any, any>;
new<K, V>(entries?: readonly (readonly [K, V])[] | null): Map<K, V>;
readonly prototype: Map<any, any>;
} And your PR fixes the issue 👍 So LGTM from me to merge this. Let's wait for other opinions. |
Is there a similar issue with |
Maybe, but it is less likely to come up since there is less of a need to use |
@urugator this |
@Nokel81 Could you also add this line to the test to make sure it is also covered? const srcValues = mobx.observable.array<string>()
const map = mobx.observable.map<string, number>(
Array.from(srcValues, (value, index) => [value, index] as const)
) |
Can we somehow wrap this up? |
@Nokel81 We're ready to go once you fix #3344 (comment) and #3344 (comment) |
Signed-off-by: Sebastian Malton <sebastian@malton.name>
Signed-off-by: Sebastian Malton <sebastian@malton.name>
4b1303c
to
d92353b
Compare
@kubk Sorry for the delay, done |
@Nokel81 Thank you 👍 |
Code change checklist
/docs
. For new functionality, at leastAPI.md
should be updatedyarn mobx test:performance
)This is really helpful after #3287 since that PR made it so that this use case failed to type check.
I added a test to make sure that this doesn't regress.