Skip to content

Commit

Permalink
fix(compiler-sfc): handle more TS built-in utilities in defineProps i…
Browse files Browse the repository at this point in the history
…nference
  • Loading branch information
yyx990803 committed Mar 28, 2023
1 parent 151a8ad commit 4355d24
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1637,6 +1637,9 @@ export default /*#__PURE__*/_defineComponent({
symbol: { type: Symbol, required: true },
extract: { type: Number, required: true },
exclude: { type: [Number, Boolean], required: true },
uppercase: { type: String, required: true },
params: { type: Array, required: true },
nonNull: { type: String, required: true },
objectOrFn: { type: [Function, Object], required: true },
union: { type: [String, Number], required: true },
literalUnion: { type: String, required: true },
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Vitest Snapshot v1
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`sfc hoist static > should enable when only script setup 1`] = `
"const foo = 'bar'
Expand Down
11 changes: 10 additions & 1 deletion packages/compiler-sfc/__tests__/compileScript.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -962,6 +962,9 @@ const emit = defineEmits(['a', 'b'])
symbol: symbol
extract: Extract<1 | 2 | boolean, 2>
exclude: Exclude<1 | 2 | boolean, 2>
uppercase: Uppercase<'foo'>
params: Parameters<(foo: any) => void>
nonNull: NonNullable<string | null>
objectOrFn: {
(): void
foo: string
Expand Down Expand Up @@ -1004,6 +1007,9 @@ const emit = defineEmits(['a', 'b'])
expect(content).toMatch(
`exclude: { type: [Number, Boolean], required: true }`
)
expect(content).toMatch(`uppercase: { type: String, required: true }`)
expect(content).toMatch(`params: { type: Array, required: true }`)
expect(content).toMatch(`nonNull: { type: String, required: true }`)
expect(content).toMatch(
`union: { type: [String, Number], required: true }`
)
Expand Down Expand Up @@ -1047,7 +1053,10 @@ const emit = defineEmits(['a', 'b'])
literalUnionMixed: BindingTypes.PROPS,
intersection: BindingTypes.PROPS,
intersection2: BindingTypes.PROPS,
foo: BindingTypes.PROPS
foo: BindingTypes.PROPS,
uppercase: BindingTypes.PROPS,
params: BindingTypes.PROPS,
nonNull: BindingTypes.PROPS
})
})

Expand Down
28 changes: 24 additions & 4 deletions packages/compiler-sfc/src/compileScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2056,31 +2056,51 @@ function inferRuntimeType(
case 'Date':
case 'Promise':
return [node.typeName.name]
case 'Record':

// TS built-in utility types
// https://www.typescriptlang.org/docs/handbook/utility-types.html
case 'Partial':
case 'Required':
case 'Readonly':
case 'Record':
case 'Pick':
case 'Omit':
case 'Required':
case 'InstanceType':
return ['Object']

case 'Uppercase':
case 'Lowercase':
case 'Capitalize':
case 'Uncapitalize':
return ['String']

case 'Parameters':
case 'ConstructorParameters':
return ['Array']

case 'NonNullable':
if (node.typeParameters && node.typeParameters.params[0]) {
return inferRuntimeType(
node.typeParameters.params[0],
declaredTypes
).filter(t => t !== 'null')
}
case 'Extract':
if (node.typeParameters && node.typeParameters.params[1]) {
return inferRuntimeType(
node.typeParameters.params[1],
declaredTypes
)
}
return ['null']
case 'Exclude':
case 'OmitThisParameter':
if (node.typeParameters && node.typeParameters.params[0]) {
return inferRuntimeType(
node.typeParameters.params[0],
declaredTypes
)
}
return ['null']
// cannot infer, fallback to null: ThisParameterType
}
}
return [`null`]
Expand Down

0 comments on commit 4355d24

Please sign in to comment.