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

Infer ref and computed TypeScript types for smarter inlining #9

Open
phoenix-ru opened this issue Nov 21, 2023 · 0 comments
Open

Infer ref and computed TypeScript types for smarter inlining #9

phoenix-ru opened this issue Nov 21, 2023 · 0 comments
Labels
experimental An experimental feature not available in official compiler

Comments

@phoenix-ru
Copy link
Owner

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:

<script setup lang="ts">
import { ref, computed } from 'vue'

const foo = ref('Hello')
const bar = ref(1)
const baz = computed<number>(() => bar.value * 2)
</script>

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

What Mode Current generation Proposal generation
{{ msg }} CSR _toDisplayString(msg.value) msg.value
{{ msg }} SSR _ssrInterpolate(msg.value) msg.value
:value="msg" SSR _ssrRenderAttr("value", msg.value) `value="${msg.value}"`

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 and computed. We can extend the analysis to also visit the value (second priority) or the type annotations (first priority) and store this information along with the BindingTypes.

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.

@phoenix-ru phoenix-ru added the experimental An experimental feature not available in official compiler label Nov 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
experimental An experimental feature not available in official compiler
Projects
None yet
Development

No branches or pull requests

1 participant