Skip to content
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

Merged
merged 9 commits into from
Aug 1, 2023

Conversation

imteekay
Copy link
Owner

@imteekay imteekay commented Jul 22, 2023

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 imteekay self-assigned this Jul 22, 2023
@imteekay
Copy link
Owner Author

@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!

Copy link
Contributor

@sandersn 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.

var s3 = 'test';

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

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

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

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 imteekay merged commit 0a0c3a5 into master Aug 1, 2023
@imteekay 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
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants