-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Leverage types for perf improvements #418
Comments
I don't think we should add flow built-in to Svelte, but instead add an API for specifying type of JavaScript to transpile (ex flow, typescript, coffeescript) and then allow setting data types. |
I highly recommend TypeScript if you're going in this direction. It has mature echo system and well-designed syntax. Moreover, it has been designed by Anders Hejlsberg (the creator of C# lang) and being maintained by Microsoft. It's being used by Microsoft (for Visual Studio Code and a bunch of other projects) as well as Angular and other open source popular projects. |
Flow has a feature called „library definition ”, where you can add type information outside the code base. Concerning TypeScript I'd like to quote Eric Elliot from The shocking secret about static types
|
@Ryuno-Ki You can do the same thing in TypeScript, it's called declaration files. Have a look at Definitly Typed it's a huge repository of type declarations for packages that are written in pure javascript. |
This came up when I was looking at how to solve #58, basically if we know the types at compile time then we can generate 100% correct type definitions for tools like typescript, flow, and (most importantly) closure-compiler. This also means that we can add dev mode warnings for setting/getting a property from the state that doesn't exist, which can catch mistakes like
I also have the personal belief that we should keep it simple and use 'object' to represent functions, symbols, and other fun types. One thing we could do if we ever add typescript/flow support is only use this syntax for javascript and let typescript/flow handle types themselves. |
I believe Babylon will be getting TS support soon. Maybe if that happened, we could allow people to write either Flow or TypeScript, and people could use real types? <script language='typescript'>
import { Options } from 'svelte';
interface Data {
foo: string;
bar: number;
complex: object;
};
export default <Options>{
data() {
return <Data>{
foo: 'x',
bar: 42,
complex: {
x: 'y'
}
};
}
};
</script> |
That's actually a bit of a complication, you can't enforce exporting a type in typescript (and flow too I think..) One way you could solve that is by wrapping the object like
It also doesn't solve the issue of jsdoc since microsoft/TypeScript#10 isn't finished yet. I also suppose if we do it this way then we can follow the pattern of continuing to deny JavaScript compile time types. |
I don't think we'd need the export to have a type — that would just be for the user's benefit, so that they'd get autocomplete when they start typing
Good point — would be nice to have a non-TS/Flow approach as well, i.e. the |
I'm still very partial to representing svelte as a function to TS/flow, because they think that the file is exporting a literally object, but it's actually exporting a full component. By wrapping the object in a function call (that happens at compile time technically) everything works smoothly in the typed world. |
Ah, think I understand you now. Can TS/Flow understand |
It depends on the editor, IntelliJ/WebStorm seem to do it well (but they implement IDE features themselves instead of using typescript's.) |
Interesting. Will have to try them sometime. |
With microsoft/TypeScript#6508 in theory we could extend the typescript compiler to add support for svelte though, but that's probably another issue. |
Looks like there has been movement within Typescript here which may help Svelte: |
@Rich-Harris Moving from pure JS to Typescript of the whole team and an in-progress project, is a big deal, actually. But simple, zero-config, type-checking is a very needful thing. I propose this simple approach: rollup.config.js svelte({
...
typeChecking: true
...
}) After it enabled, Svelte would check default values and will check state changes to have same types. If not, Svelte will produce a warning. {qux} <!-- could be Any type -->
<script>
export default {
data () {
return {
foo: '', // should be a sting
bar: false, // should be a boolean
baz: [] // should be an array
};
}
};
</script> This's simplest approach without hitting new props or langs. |
I totally agree with @PaulMaly , optional type checking will be awesome. Typescript or die it's not an argument. |
Are the any plans for this issue? Smething like React's PropTypes can be nice (not sure about the implementation... |
Closing as most of the discussion predates Svelte 3, so the thoughts on performance improvements are out of date by now. Regarding type-checking, there's good support for TypeScript now, and it's also possible to leverage JSDocs to do type checking without transitioning to TypeScript. |
Moving #415 (comment) into its own issue.
There are certain places where Svelte could use type information to generate more compact or efficient code.
The big win is probably in
recompute
(formerlyapplyComputations
) where we can shortcut the conditional that determines whether a given computed value's dependencies may have changed. There are other cases as well, e.g. this......results in this code...
...which could easily become this, if we had confidence that
foo
was a string:(The unnecessary parens are a separate matter — they're included defensively but unnecessarily in most cases. A minifier can see to those though, whereas it can't remove the leading
"" +
).The easiest way to get the type information would be to analyse the template — if
{{a.b}}
is encountered we knowa
is non-primitive, if{{a()}}
is encountered we know it's a function, and if{{#each a as x}}
is encountered we knowa
is array-like. (Or rather, we know that the component would fail anyway if those assumptions proved incorrect, in which case we lose nothing by making the assumption.)There are other bits of static analysis we could do but they are far less straightforward and probably prone to uncertainty, which we should avoid.
We could also allow users to specify type information:
This would only be used for SA, and removed from the compiled output. In dev mode, we could add warnings if the wrong type is supplied.
Finally, it might be possible to use Flow to more sophisticated type inference. No idea how that would work in practice though.
The text was updated successfully, but these errors were encountered: