Dark Mode

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Allow var to have multiple declarations#2

Merged
imteekay merged 9 commits intomasterfrom
var-with-multiple-declarations
Aug 1, 2023
Merged

Allow var to have multiple declarations#2
imteekay merged 9 commits intomasterfrom
var-with-multiple-declarations

Conversation

Copy link
Owner

imteekay commented Jul 22, 2023 *
edited
Loading

Examples

var s = 'test';

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

var s = 'test';

!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

var s: string = 'test';

!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 = 'test';
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: number = 2;
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: string = 'test';
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;

imteekay self-assigned this Jul 22, 2023
Copy link
Owner Author

imteekay commented Jul 26, 2023

@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 reviewed Jul 31, 2023
Copy link
Contributor

sandersn left a 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.

imteekay reacted with hooray emoji
var s3 = 'test';

var s4: string = 'test';
var s4: number = 2;
Copy link
Contributor

sandersn Jul 31, 2023

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.

Copy link
Owner Author

imteekay Aug 1, 2023

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!

Copy link
Contributor

sandersn Aug 1, 2023

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.

imteekay reacted with thumbs up emoji
imteekay merged commit 0a0c3a5 into master Aug 1, 2023
imteekay deleted the var-with-multiple-declarations branch August 1, 2023 22:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

1 more reviewer

sandersn sandersn left review comments

Reviewers whose approvals may not affect merge requirements

Assignees

imteekay

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

2 participants