Skip to content

Commit

Permalink
fix(better-define): infer TS Extract&Exclude runtime type
Browse files Browse the repository at this point in the history
  • Loading branch information
topsmart20 committed Feb 23, 2023
1 parent b830046 commit cf70fc2
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 2 deletions.
6 changes: 6 additions & 0 deletions .changeset/red-waves-tease.md
@@ -0,0 +1,6 @@
---
'@vue-macros/better-define': patch
'@vue-macros/api': patch
---

infer TS `Extract` and `Exclude` runtime type
29 changes: 27 additions & 2 deletions packages/api/src/vue/utils.ts
Expand Up @@ -67,11 +67,36 @@ export async function inferRuntimeType(
case 'Readonly':
case 'Pick':
case 'Omit':
case 'Exclude':
case 'Extract':
case 'Required':
case 'InstanceType':
return ['Object']

case 'Extract':
if (
node.type.typeParameters &&
node.type.typeParameters.params[1]
) {
const t = await resolveTSReferencedType({
scope: node.scope,
type: node.type.typeParameters.params[1],
})
if (isTSExports(t)) return ['Object']
else if (t) return inferRuntimeType(t)
}
return ['null']
case 'Exclude':
if (
node.type.typeParameters &&
node.type.typeParameters.params[0]
) {
const t = await resolveTSReferencedType({
scope: node.scope,
type: node.type.typeParameters.params[0],
})
if (isTSExports(t)) return ['Object']
else if (t) return inferRuntimeType(t)
}
return ['null']
}
}
return [`null`]
Expand Down
58 changes: 58 additions & 0 deletions packages/better-define/tests/__snapshots__/fixtures.test.ts.snap
Expand Up @@ -586,6 +586,64 @@ export { specialKey as default };
"
`;

exports[`fixtures > tests/fixtures/ts-utility-types.vue > isProduction = false 1`] = `
"import { defineComponent } from 'vue';
var _sfc_main = /* @__PURE__ */ defineComponent({
__name: \\"ts-utility-types\\",
props: {
\\"foo\\": { type: String, required: true },
\\"bar\\": { type: String, required: true }
},
setup(__props) {
return () => {
};
}
});
var _export_sfc = (sfc, props) => {
const target = sfc.__vccOpts || sfc;
for (const [key, val] of props) {
target[key] = val;
}
return target;
};
var tsUtilityTypes = /* @__PURE__ */ _export_sfc(_sfc_main, [__FILE__]);
export { tsUtilityTypes as default };
"
`;

exports[`fixtures > tests/fixtures/ts-utility-types.vue > isProduction = true 1`] = `
"import { defineComponent } from 'vue';
var _sfc_main = /* @__PURE__ */ defineComponent({
__name: \\"ts-utility-types\\",
props: {
\\"foo\\": null,
\\"bar\\": null
},
setup(__props) {
return () => {
};
}
});
var _export_sfc = (sfc, props) => {
const target = sfc.__vccOpts || sfc;
for (const [key, val] of props) {
target[key] = val;
}
return target;
};
var tsUtilityTypes = /* @__PURE__ */ _export_sfc(_sfc_main, [__FILE__]);
export { tsUtilityTypes as default };
"
`;

exports[`fixtures > tests/fixtures/union.vue > isProduction = false 1`] = `
"import { defineComponent } from 'vue';
Expand Down
14 changes: 14 additions & 0 deletions packages/better-define/tests/fixtures/ts-utility-types.vue
@@ -0,0 +1,14 @@
<script setup lang="ts">
type Union = '1' | '2' | '3'
type FooExtract = '1' | '2'
type Foo = Extract<Union, FooExtract>
type BarExclude = '2'
type Bar = Exclude<Union, BarExclude>
defineProps<{
foo: Foo
bar: Bar
}>()
</script>

0 comments on commit cf70fc2

Please sign in to comment.