Navigation Menu

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(compiler-sfc): infer TS Extract&Exclude runtime type #7339

Merged
merged 2 commits into from Mar 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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]) {
Copy link
Member

@yyx990803 yyx990803 Mar 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: this will result in a wider type than it should in the case of Exclude<string | number, string>, however performing the correct analysis for all possible cases is impossible without relying on actual TS inference, so the best we can do is to infer a wider type for now.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's unlikely to be able to figure out the exact type unless we're using the TS compiler.

return inferRuntimeType(
node.typeParameters.params[0],
declaredTypes
)
}
return ['null']
}
}
return [`null`]
Expand Down