Skip to content
52 changes: 52 additions & 0 deletions packages/language-core/lib/codegen/globalTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,58 @@ export function generateGlobalTypes(options: VueCompilerOptions) {
type __VLS_IsAny<T> = 0 extends 1 & T ? true : false;
type __VLS_PickNotAny<A, B> = __VLS_IsAny<A> extends true ? B : A;
type __VLS_SpreadMerge<A, B> = Omit<A, keyof B> & B;
type __VLS_PublicInstanceWithoutExpose<T> =
T extends import('${lib}').CreateComponentPublicInstanceWithMixins<
infer P,
infer B,
infer D,
infer C extends import('${lib}').ComputedOptions,
infer M extends import('${lib}').MethodOptions,
infer Mixin extends import('${lib}').ComponentOptionsMixin,
infer Extends extends import('${lib}').ComponentOptionsMixin,
infer E extends import('${lib}').EmitsOptions,
infer PublicProps,
infer Defaults,
infer MakeDefaultsOptional extends boolean,
infer I extends import('${lib}').ComponentInjectOptions,
infer S extends import('${lib}').SlotsType,
infer LC extends Record<string, import('${lib}').Component>,
infer Directives extends Record<string, import('${lib}').Directive>,
infer Exposed extends string,
infer TypeRefs extends Record<string, unknown>,
infer TypeEl extends Element,
infer Provide extends import('${lib}').ComponentProvideOptions,
infer PublicMixin,
infer PublicP,
infer PublicB
>
? '' extends Exposed
? T
: import('${lib}').CreateComponentPublicInstanceWithMixins<
P,
B,
D,
C,
M,
Mixin,
Extends,
E,
PublicProps,
Defaults,
MakeDefaultsOptional,
I,
S,
LC,
Directives,
'',
TypeRefs,
TypeEl,
Provide,
PublicMixin,
PublicP,
PublicB
>
: T;
type __VLS_WithComponent<N0 extends string, LocalComponents, Self, N1 extends string, N2 extends string, N3 extends string> =
N1 extends keyof LocalComponents ? { [K in N0]: LocalComponents[N1] } :
N2 extends keyof LocalComponents ? { [K in N0]: LocalComponents[N2] } :
Expand Down
8 changes: 7 additions & 1 deletion packages/language-core/lib/codegen/script/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,13 @@ function* generateTemplateCtx(
exps.push(`globalThis`);
}
if (options.sfc.script?.src || options.scriptRanges?.exportDefault) {
exps.push(`{} as InstanceType<__VLS_PickNotAny<typeof __VLS_self, new () => {}>>`);
yield `type __VLS_SelfInstance = InstanceType<__VLS_PickNotAny<typeof __VLS_self, new () => {}>>${endOfLine}`;
if (options.scriptRanges?.componentOptions?.hasExposeOption) {
exps.push(`{} as __VLS_PublicInstanceWithoutExpose<__VLS_SelfInstance>`);
}
else {
exps.push(`{} as __VLS_SelfInstance`);
}
}
else {
exps.push(`{} as import('${options.vueCompilerOptions.lib}').ComponentPublicInstance`);
Expand Down
6 changes: 6 additions & 0 deletions packages/language-core/lib/parsers/scriptRanges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export function parseScriptRanges(ts: typeof import('typescript'), ast: ts.Sourc
directives: TextRange | undefined;
name: TextRange | undefined;
inheritAttrs: string | undefined;
hasExposeOption: boolean;
}
| undefined;

Expand Down Expand Up @@ -57,6 +58,7 @@ export function parseScriptRanges(ts: typeof import('typescript'), ast: ts.Sourc
let directivesOptionNode: ts.ObjectLiteralExpression | undefined;
let nameOptionNode: ts.Expression | undefined;
let inheritAttrsOption: string | undefined;
let hasExposeOption = false;
ts.forEachChild(obj, node => {
if (ts.isPropertyAssignment(node) && ts.isIdentifier(node.name)) {
const name = _getNodeText(node.name);
Expand All @@ -72,6 +74,9 @@ export function parseScriptRanges(ts: typeof import('typescript'), ast: ts.Sourc
else if (name === 'inheritAttrs') {
inheritAttrsOption = _getNodeText(node.initializer);
}
else if (name === 'expose') {
hasExposeOption = true;
}
}
});
componentOptions = {
Expand All @@ -83,6 +88,7 @@ export function parseScriptRanges(ts: typeof import('typescript'), ast: ts.Sourc
directives: directivesOptionNode ? _getStartEnd(directivesOptionNode) : undefined,
name: nameOptionNode ? _getStartEnd(nameOptionNode) : undefined,
inheritAttrs: inheritAttrsOption,
hasExposeOption,
};
}
}
Expand Down
42 changes: 42 additions & 0 deletions test-workspace/tsc/passedFixtures/vue3/#5069/main.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<script lang="ts">
import { defineComponent, ref } from 'vue'

export default defineComponent({
setup() {
const setup1 = ref('setup1');
const setup2 = ref('setup2');
return { setup1, setup2 };
},
data() {
return {
data1: 1
}
},
props: {
props1: {
type: String,
}
},
computed: {
computed1(): string {
return this.setup1.toUpperCase();
}
},
methods: {
methods1(): string {
return `methods1`;
}
},
expose: ['setup1']
});
</script>

<template>
<div @click="methods1()">
{{ setup1 }}
{{ setup2 }}
{{ data1 }}
{{ props1 }}
{{ computed1 }}
</div>
</template>
Loading