-
-
Notifications
You must be signed in to change notification settings - Fork 0
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 var to have multiple declarations #2
Conversation
…its typename and its expression type
@sandersn I finally created the PR related to that exercise we discussed a couple of weeks ago. With this exercise, I could the benefits of using some kind of "caching system" to prevent recreating types that were created before. But I tried to make it as simple as possible for now. Appreciate any feedback! |
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. Nice clean code.
var s3 = 'test'; | ||
|
||
var s4: string = 'test'; | ||
var s4: number = 2; |
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.
interesting choice not to have an error on 2
. Probably the right one, to avoid inundating the user with errors.
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.
Do you mean this?
var s4: string = 'test';
var s4: number = 2; // this has an error because it should actually be a string
var s4: string = 's'; // it doesn't produce an error because it compares only with the first var declaration
If so, I got this behavior from TS itself 😅
to avoid inundating the user with errors.
^ That makes sense to me!
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.
I meant no error on the 2
in var s4: number = 2
, even though s4
actually has type string. It makes sense because of the explicit type annotation : number
.
Examples
Error message
Example of an error:
Error
: "Subsequent variable declarations must have the same type. Variable 's' must be of type 'string', but here has type 'number'."Logic
!001
It doesn't have a variable declaration of
s
before it and it doesn't have atypename
, so it should just need to return the type related to the expression'test'
, which'sstring
!002
It doesn't have a variable declaration of
s
before it but it has atypename
and the type of the expression, so it should compare both and return thetypename
!003
It has a variable declaration of
s
before it and it has thetypename
and the type of the expression, so it should compare itstypename
with the first variable declaration type!004
It has a variable declaration of
s
before it and it has only the type of the expression, so it should compare the expression type with the first variable declaration type!005
It has a variable declaration of
s
before it and it has thetypename
and the type of the expression, so it should compare itstypename
with the first variable declaration type. In a mismatch case, it should generate the type error. Subsequent variable declarations that follow the same type as the first one, should not generate an error