Skip to content

Commit 6391daf

Browse files
authored
fix(compiler-sfc): infer TS Extract&Exclude runtime type (#7339)
closes #7337 closes #6252
1 parent 3a7572c commit 6391daf

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap

+2
Original file line numberDiff line numberDiff line change
@@ -1619,6 +1619,8 @@ export default /*#__PURE__*/_defineComponent({
16191619
alias: { type: Array, required: true },
16201620
method: { type: Function, required: true },
16211621
symbol: { type: Symbol, required: true },
1622+
extract: { type: Number, required: true },
1623+
exclude: { type: [Number, Boolean], required: true },
16221624
objectOrFn: { type: [Function, Object], required: true },
16231625
union: { type: [String, Number], required: true },
16241626
literalUnion: { type: String, required: true },

packages/compiler-sfc/__tests__/compileScript.spec.ts

+8
Original file line numberDiff line numberDiff line change
@@ -960,6 +960,8 @@ const emit = defineEmits(['a', 'b'])
960960
alias: Alias
961961
method(): void
962962
symbol: symbol
963+
extract: Extract<1 | 2 | boolean, 2>
964+
exclude: Exclude<1 | 2 | boolean, 2>
963965
objectOrFn: {
964966
(): void
965967
foo: string
@@ -997,6 +999,10 @@ const emit = defineEmits(['a', 'b'])
997999
expect(content).toMatch(
9981000
`objectOrFn: { type: [Function, Object], required: true },`
9991001
)
1002+
expect(content).toMatch(`extract: { type: Number, required: true }`)
1003+
expect(content).toMatch(
1004+
`exclude: { type: [Number, Boolean], required: true }`
1005+
)
10001006
expect(content).toMatch(
10011007
`union: { type: [String, Number], required: true }`
10021008
)
@@ -1031,6 +1037,8 @@ const emit = defineEmits(['a', 'b'])
10311037
method: BindingTypes.PROPS,
10321038
symbol: BindingTypes.PROPS,
10331039
objectOrFn: BindingTypes.PROPS,
1040+
extract: BindingTypes.PROPS,
1041+
exclude: BindingTypes.PROPS,
10341042
union: BindingTypes.PROPS,
10351043
literalUnion: BindingTypes.PROPS,
10361044
literalUnionNumber: BindingTypes.PROPS,

packages/compiler-sfc/src/compileScript.ts

+17-2
Original file line numberDiff line numberDiff line change
@@ -2057,11 +2057,26 @@ function inferRuntimeType(
20572057
case 'Readonly':
20582058
case 'Pick':
20592059
case 'Omit':
2060-
case 'Exclude':
2061-
case 'Extract':
20622060
case 'Required':
20632061
case 'InstanceType':
20642062
return ['Object']
2063+
2064+
case 'Extract':
2065+
if (node.typeParameters && node.typeParameters.params[1]) {
2066+
return inferRuntimeType(
2067+
node.typeParameters.params[1],
2068+
declaredTypes
2069+
)
2070+
}
2071+
return ['null']
2072+
case 'Exclude':
2073+
if (node.typeParameters && node.typeParameters.params[0]) {
2074+
return inferRuntimeType(
2075+
node.typeParameters.params[0],
2076+
declaredTypes
2077+
)
2078+
}
2079+
return ['null']
20652080
}
20662081
}
20672082
return [`null`]

0 commit comments

Comments
 (0)