Skip to content

TypeScript: Infer the type for $: assignment #172

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

Closed
Ciantic opened this issue Jun 8, 2020 · 1 comment
Closed

TypeScript: Infer the type for $: assignment #172

Ciantic opened this issue Jun 8, 2020 · 1 comment
Assignees
Labels
next-major will be fixed in the next major

Comments

@Ciantic
Copy link

Ciantic commented Jun 8, 2020

Is your feature request related to a problem? Please describe.

Take following code:

  let count = 5;
  let doubled: number;
  $: doubled = count * 2;

It's unnecessarily verbose, the TS could infer the type for us if the preprocessor could turn this in to a appropriate TS.

If you leave the let doubled: number; out it says it's any and with TS that is not wanted.

Describe the solution you'd like

Ideal would be to infer the type correctly so it could be written just:

  let count = 5;
  $: doubled = count * 2;

  // TypeScript type-checker could be made to see this code as:
  let doubled = count * 2;
  // so that type inference would work correclty.

In a case where even TypeScript can't infer, then it would be second most natural to use this format:

  let count = 5;
  $: doubled: number = count * 2;

In a way for TypeScript the $: appears as let

It was mentioned in here: sveltejs/svelte#4518 that preprocessor does the conversion first to typescript. Would it be possible to do something so that type could be inferred?

@kaisermann kaisermann added the next-major will be fixed in the next major label Jun 19, 2020
@kaisermann kaisermann self-assigned this Jun 19, 2020
@kaisermann
Copy link
Member

Fixed in v4 as we're no long doing any kind of type-checking in the preprocessor 😁 .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
next-major will be fixed in the next major
Projects
None yet
Development

No branches or pull requests

2 participants