Skip to content

fix(language-core): prevent the generation of generics in JS #4836

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

Merged
merged 12 commits into from
Oct 24, 2024
1 change: 1 addition & 0 deletions packages/language-core/lib/codegen/globalTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export function generateGlobalTypes(lib: string, target: number, strictTemplates
'__ctx' extends keyof __VLS_PickNotAny<K, {}> ? K extends { __ctx?: infer Ctx } ? Ctx : never : any
, T extends (props: any, ctx: infer Ctx) => any ? Ctx : any
>>;
type __VLS_UseTemplateRef<T> = Readonly<import('${lib}').ShallowRef<T | null>>;

function __VLS_getVForSourceType(source: number): [number, number, number][];
function __VLS_getVForSourceType(source: string): [string, number, number][];
Expand Down
21 changes: 20 additions & 1 deletion packages/language-core/lib/codegen/script/scriptSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,12 @@ function* generateSetupFunction(
]);
}
}
const isTs = options.lang !== 'js' && options.lang !== 'jsx';
for (const { define } of scriptSetupRanges.templateRefs) {
if (define?.arg) {
if (!define?.arg) {
continue;
}
if (isTs) {
setupCodeModifies.push([
[
`<__VLS_TemplateResult['refs'][`,
Expand All @@ -223,6 +227,21 @@ function* generateSetupFunction(
define.exp.end
]);
}
else {
setupCodeModifies.push([
[`(`],
define.start,
define.start
], [
[
` as __VLS_UseTemplateRef<__VLS_TemplateResult['refs'][`,
generateSfcBlockSection(scriptSetup, define.arg.start, define.arg.end, codeFeatures.navigation),
`]>)`
],
define.end,
define.end
]);
}
}
setupCodeModifies = setupCodeModifies.sort((a, b) => a[1] - b[1]);

Expand Down
1 change: 1 addition & 0 deletions test-workspace/tsc/passedFixtures/vue2/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"../vue3/#4777",
"../vue3/#4820",
"../vue3/#4822",
"../vue3/#4826",
"../vue3/#4828",
"../vue3/components",
"../vue3/defineEmits",
Expand Down
1 change: 1 addition & 0 deletions test-workspace/tsc/passedFixtures/vue3.4/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"../vue3/#3820",
"../vue3/#4777",
"../vue3/#4820",
"../vue3/#4826",
"../vue3/#4828",
"../vue3/rootEl",
"../vue3/templateRef",
Expand Down
10 changes: 10 additions & 0 deletions test-workspace/tsc/passedFixtures/vue3/#4826/child.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<script setup>
import { useTemplateRef } from 'vue';

const a = useTemplateRef('a');
defineExpose({ a });
</script>

<template>
<a ref="a" />
</template>
12 changes: 12 additions & 0 deletions test-workspace/tsc/passedFixtures/vue3/#4826/main.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<script setup lang="ts">
import { useTemplateRef } from 'vue';
import { exactType } from '../../shared';
import Child from './child.vue';

const child = useTemplateRef('child');
exactType(child.value?.a, {} as HTMLAnchorElement | null | undefined);
</script>

<template>
<Child ref="child" />
</template>