Infer ref
and computed
TypeScript types for smarter inlining
#9
Labels
experimental
An experimental feature not available in official compiler
Situation
When using
<script setup lang="ts">
we can make assumptions regarding binding types. For instance, we can confidently infer the types of bindings in this example:Proposal
fervid
can trust the user's TypeScript setup to catch type errors. In the future, when stc is mature enough, it will be used for type inference and checking.Knowing the binding TS type, we can further optimize the generation of Interpolations and SSR attributes:
const msg = ref('') // string
By having the knowledge of types we can omit unnecessary runtime checks and also ship less code.
Possible implementation
The transformer already detects Vue-specific symbols, such as
ref
andcomputed
. We can extend the analysis to also visit the value (second priority) or the type annotations (first priority) and store this information along with theBindingTypes
.Considering the fact that TS types are complex and inference may not be trivial, the TS type must be an optional hint for the compiler.
When no such type can be inferred (or when outside TS), the compiler should fall back to default code generation.
The text was updated successfully, but these errors were encountered: