Skip to content

Commit

Permalink
fix: ts type
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz committed May 18, 2023
1 parent 4579264 commit abce65f
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/api/src/ts/resolve.ts
Expand Up @@ -17,6 +17,7 @@ import {
type TSTypeElement,
type TSTypeOperator,
type TSTypeReference,
type TSUnionType,
type TemplateElement,
type TemplateLiteral,
} from '@babel/types'
Expand Down Expand Up @@ -185,7 +186,7 @@ export function resolveTypeElements(
export async function resolveTSIndexedAccessType(
{ scope, type }: TSResolvedType<TSIndexedAccessType>,
stacks: TSResolvedType<any>[] = []
) {
): Promise<{ type: TSUnionType; scope: TSScope } | undefined> {
const object = await resolveTSReferencedType(
{ type: type.objectType, scope },
stacks
Expand Down
1 change: 1 addition & 0 deletions packages/define-prop/src/core/index.ts
Expand Up @@ -80,6 +80,7 @@ export async function transformDefineProp(
async function resolveTSType(type: TSType) {
const resolved = await resolveTSReferencedType({
scope: {
kind: 'file',
filePath: id,
content: scriptSetup!.content,
ast: setupAst.body,
Expand Down
57 changes: 57 additions & 0 deletions packages/define-prop/tests/__snapshots__/fixtures.test.ts.snap
Expand Up @@ -39,3 +39,60 @@ const unknownType = __MACROS_toRef(__MACROS_props, \\"unknownType\\")
export { basic as default };
"
`;
exports[`fixtures > tests/fixtures/kevin-edition/basic.vue 1`] = `
"var basic = \`<script setup lang=\\"ts\\">
const __MACROS_props = defineProps({
\\"foo\\": { type: String },
\\"bar\\": { type: String, ...{
type: String,
required: true,
default: 'bar',
} },
\\"baz\\": { type: [String, Number], ...{
required: true,
default: () => [1, 2, 3],
} },
\\"qux\\": { type: [String, Number] },
\\"quux\\": { type: Boolean, ...{ default: true } },
\\"quuz\\": null,
\\"unknownType\\": { type: Boolean, skipCheck: true },
});
import { toRef as __MACROS_toRef } from \\"vue\\";
import { expectTypeOf } from 'expect-type'
import type { ComputedRef } from 'vue'
import type { Qux } from '../types'
import { defineProp } from '../../../macros'
// defineProp(prop_name)
const foo = __MACROS_toRef(__MACROS_props, \\"foo\\")
expectTypeOf(foo).toEqualTypeOf<ComputedRef<string | undefined>>()
// defineProp(prop_name, options)
const bar = __MACROS_toRef(__MACROS_props, \\"bar\\")
expectTypeOf(bar).toEqualTypeOf<ComputedRef<string>>()
// defineProp(prop_name, options)
const baz = __MACROS_toRef(__MACROS_props, \\"baz\\")
expectTypeOf(baz).toEqualTypeOf<ComputedRef<string | number>>()
// defineProp(prop_name)
__MACROS_toRef(__MACROS_props, \\"qux\\")
// defineProp(prop_name, options)
__MACROS_toRef(__MACROS_props, \\"quux\\")
// infer prop name from variable name
// const prop_name = defineProp()
const quuz = __MACROS_toRef(__MACROS_props, \\"quuz\\")
expectTypeOf(quuz).toEqualTypeOf<ComputedRef<unknown>>()
console.log(quuz)
const unknownType = __MACROS_toRef(__MACROS_props, \\"unknownType\\")
</script>
\`;
export { basic as default };
"
`;

0 comments on commit abce65f

Please sign in to comment.