-
-
Notifications
You must be signed in to change notification settings - Fork 0
Conversation
Examples
var s1: string = 'test';
var s2 = 'test';
var s2: string = 'test';
var s3: number = 2;
var s3 = 'test';
var s4: string = 'test';
var s4: number = 2;
var s4 = 'test';
var s5 = 'test';
var s5: string = 2;
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 a typename, so it should just need to return the type related to the expression 'test', which's string
!002
It doesn't have a variable declaration of s before it but it has a typename and the type of the expression, so it should compare both and return the typename
!003
It has a variable declaration of s before it and it has the typename and the type of the expression, so it should compare its typename with the first variable declaration type
var s: string = 'test';
!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
var s = 'test';
!005
It has a variable declaration of s before it and it has the typename and the type of the expression, so it should compare its typename 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
var s: number = 2;
var s = 'test';
``
`
### !006
It has a `typename` that matches the first variable declaration type but its expressions type mismatches its `typename`
```ts
var s = 'test';
var s: string = 2;
|
@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! |
sandersn
left a comment
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: 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.