-
Notifications
You must be signed in to change notification settings - Fork 171
[proposal] fix: type check issues on encod/decode arguments #139
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
Conversation
codecov-io
commented
Oct 10, 2020
Codecov Report
@@ Coverage Diff @@ ## master #139 +/- ## ======================================= Coverage 98.30% 98.30% ======================================= Files 15 15 Lines 943 943 Branches 205 205 ======================================= Hits 927 927 Misses 16 16
Continue to review full report at Codecov.
|
3 similar comments
codecov-commenter
commented
Oct 10, 2020
Codecov Report
@@ Coverage Diff @@ ## master #139 +/- ## ======================================= Coverage 98.30% 98.30% ======================================= Files 15 15 Lines 943 943 Branches 205 205 ======================================= Hits 927 927 Misses 16 16
Continue to review full report at Codecov.
|
Codecov Report
@@ Coverage Diff @@ ## master #139 +/- ## ======================================= Coverage 98.30% 98.30% ======================================= Files 15 15 Lines 943 943 Branches 205 205 ======================================= Hits 927 927 Misses 16 16
Continue to review full report at Codecov.
|
codecov-commenter
commented
Oct 10, 2020
Codecov Report
@@ Coverage Diff @@ ## master #139 +/- ## ======================================= Coverage 98.30% 98.30% ======================================= Files 15 15 Lines 943 943 Branches 205 205 ======================================= Hits 927 927 Misses 16 16
Continue to review full report at Codecov.
|
Codecov Report
@@ Coverage Diff @@ ## master #139 +/- ## ======================================= Coverage 98.30% 98.30% ======================================= Files 15 15 Lines 943 943 Branches 205 205 ======================================= Hits 927 927 Misses 16 16
Continue to review full report at Codecov.
|
46d5f32
to
85db7c0
Compare
This commit intends to fix the issue reported on msgpack#136
85db7c0
to
b623c69
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unless giving undefined
as default type, the following code will cause type mismatch errors:
encode(someValue, { sortKeys: true })
Even though this error can be avoidable by giving explicit type annotation to the code above like encode(someValue, { sortKeys: true } as EncodeOptions)
, this will be braking change of the API.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me. Thanks.
Uh oh!
There was an error while loading. Please reload this page.
About
This PR intends to fix the issue reported on #136 , where I described the workaround for avoiding type check error in TypeScript.
The goal of this PR is making msgpack-javascript available from TypeScript both with/without enabling
strictNullCheck
compile option.Since I don't know the actual intention of the original implementer ( @grantila ), the change in this PR might be controversial.
So, though it would be appreciated if you welcome this PR, I highly recommend to ask @grantila 's opinion.
Background
Issue of the current implementation
msgpack-javascript/src/context.ts
Line 3 in 8ba75f8
I think this implementation has some issues.
issue of
SplitTypes
whenstrictNullCheck
is disabled:This short code describes the issue.
As I wrote in the inline comment, given
type Split<T, U> = U extends T ? U : Exclude<T, U>
,Split<T, undefined>
is always evaluated asnever
unlessT
isundefined
becauseundefined
is subtype of all other types whenstrictNullCheck
is disabled. (see TypeScript: Handbook - Basic Types)This behavior is the cause of the issue that i reported in #136.
Lets say, if you write the following code in this circumstance:
This code wouldn't be able to compiled as I reported.
The reason can be seen by gradually evaluating
encode({myType: new MyType<any>()}, { extensionCodec, context });
:This code emulates the issue of the actual code.
above issue can be resolved when
strictNullCheck
is enabled, but ...So, by enabling
strictNullCheck
, this issue can be solved as I reported in #136.But, this is not ideal because client code is forced to enable
strictNullCheck
.I was wondering if it's possible to achieve the goal (using msgpack-javascript by TypeScript without enabling
strictNullCheck
and without changing its API).Then, I found one weird behavior of
SplitTypes
when thestrictNullCheck
is enabled.(actual code is here)
For me, given the name
SplitUndefined
, I felt weird ifSplitUndefined<number | undefined>
is evaluated asundefined
. Rather, it seems natural forSplitUndefined<number | undefined>
to be evaluated asnumber
, for me.Besides,
Split<T, U> = U extends T ? U : Exclude<T, U>
seems logically incorrect.Given U is not subtype of T, T won't be the Union Type that contains U. Thus,
Exclude<T, U>
never excludesU
.So, I started to think this is the bug that @grantila ( original implementer of this code ) didn't intend.
It's really long and winding explanation, that was the story I addressed this PR.
About the changes
Essential change in this PR is the following change.
https://github.com/msgpack/msgpack-javascript/pull/139/files#diff-a7af09f7fa8732629ff9579258b5584bR3
The behavior of this Code is like following code:
This code behave same regardless of the state of
strictNullCheck
strictNullCheck
is enabledstrictNullCheck
is disabledThe rest of changes are necessary changes so that type system works properly.