Skip to content

Commit

Permalink
fix(compiler-sfc): infer TS Extract&Exclude runtime type (#7339)
Browse files Browse the repository at this point in the history
closes #7337
closes #6252
  • Loading branch information
sxzz committed Mar 28, 2023
1 parent 3a7572c commit 6391daf
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
Expand Up @@ -1619,6 +1619,8 @@ export default /*#__PURE__*/_defineComponent({
alias: { type: Array, required: true },
method: { type: Function, required: true },
symbol: { type: Symbol, required: true },
extract: { type: Number, required: true },
exclude: { type: [Number, Boolean], required: true },
objectOrFn: { type: [Function, Object], required: true },
union: { type: [String, Number], required: true },
literalUnion: { type: String, required: true },
Expand Down
8 changes: 8 additions & 0 deletions packages/compiler-sfc/__tests__/compileScript.spec.ts
Expand Up @@ -960,6 +960,8 @@ const emit = defineEmits(['a', 'b'])
alias: Alias
method(): void
symbol: symbol
extract: Extract<1 | 2 | boolean, 2>
exclude: Exclude<1 | 2 | boolean, 2>
objectOrFn: {
(): void
foo: string
Expand Down Expand Up @@ -997,6 +999,10 @@ const emit = defineEmits(['a', 'b'])
expect(content).toMatch(
`objectOrFn: { type: [Function, Object], required: true },`
)
expect(content).toMatch(`extract: { type: Number, required: true }`)
expect(content).toMatch(
`exclude: { type: [Number, Boolean], required: true }`
)
expect(content).toMatch(
`union: { type: [String, Number], required: true }`
)
Expand Down Expand Up @@ -1031,6 +1037,8 @@ const emit = defineEmits(['a', 'b'])
method: BindingTypes.PROPS,
symbol: BindingTypes.PROPS,
objectOrFn: BindingTypes.PROPS,
extract: BindingTypes.PROPS,
exclude: BindingTypes.PROPS,
union: BindingTypes.PROPS,
literalUnion: BindingTypes.PROPS,
literalUnionNumber: BindingTypes.PROPS,
Expand Down
19 changes: 17 additions & 2 deletions packages/compiler-sfc/src/compileScript.ts
Expand Up @@ -2057,11 +2057,26 @@ function inferRuntimeType(
case 'Readonly':
case 'Pick':
case 'Omit':
case 'Exclude':
case 'Extract':
case 'Required':
case 'InstanceType':
return ['Object']

case 'Extract':
if (node.typeParameters && node.typeParameters.params[1]) {
return inferRuntimeType(
node.typeParameters.params[1],
declaredTypes
)
}
return ['null']
case 'Exclude':
if (node.typeParameters && node.typeParameters.params[0]) {
return inferRuntimeType(
node.typeParameters.params[0],
declaredTypes
)
}
return ['null']
}
}
return [`null`]
Expand Down

0 comments on commit 6391daf

Please sign in to comment.