Skip to content

Commit 4355d24

Browse files
committed
fix(compiler-sfc): handle more TS built-in utilities in defineProps inference
1 parent 151a8ad commit 4355d24

File tree

4 files changed

+38
-6
lines changed

4 files changed

+38
-6
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1637,6 +1637,9 @@ export default /*#__PURE__*/_defineComponent({
16371637
symbol: { type: Symbol, required: true },
16381638
extract: { type: Number, required: true },
16391639
exclude: { type: [Number, Boolean], required: true },
1640+
uppercase: { type: String, required: true },
1641+
params: { type: Array, required: true },
1642+
nonNull: { type: String, required: true },
16401643
objectOrFn: { type: [Function, Object], required: true },
16411644
union: { type: [String, Number], required: true },
16421645
literalUnion: { type: String, required: true },

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Vitest Snapshot v1
1+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

33
exports[`sfc hoist static > should enable when only script setup 1`] = `
44
"const foo = 'bar'

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -962,6 +962,9 @@ const emit = defineEmits(['a', 'b'])
962962
symbol: symbol
963963
extract: Extract<1 | 2 | boolean, 2>
964964
exclude: Exclude<1 | 2 | boolean, 2>
965+
uppercase: Uppercase<'foo'>
966+
params: Parameters<(foo: any) => void>
967+
nonNull: NonNullable<string | null>
965968
objectOrFn: {
966969
(): void
967970
foo: string
@@ -1004,6 +1007,9 @@ const emit = defineEmits(['a', 'b'])
10041007
expect(content).toMatch(
10051008
`exclude: { type: [Number, Boolean], required: true }`
10061009
)
1010+
expect(content).toMatch(`uppercase: { type: String, required: true }`)
1011+
expect(content).toMatch(`params: { type: Array, required: true }`)
1012+
expect(content).toMatch(`nonNull: { type: String, required: true }`)
10071013
expect(content).toMatch(
10081014
`union: { type: [String, Number], required: true }`
10091015
)
@@ -1047,7 +1053,10 @@ const emit = defineEmits(['a', 'b'])
10471053
literalUnionMixed: BindingTypes.PROPS,
10481054
intersection: BindingTypes.PROPS,
10491055
intersection2: BindingTypes.PROPS,
1050-
foo: BindingTypes.PROPS
1056+
foo: BindingTypes.PROPS,
1057+
uppercase: BindingTypes.PROPS,
1058+
params: BindingTypes.PROPS,
1059+
nonNull: BindingTypes.PROPS
10511060
})
10521061
})
10531062

packages/compiler-sfc/src/compileScript.ts

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2056,31 +2056,51 @@ function inferRuntimeType(
20562056
case 'Date':
20572057
case 'Promise':
20582058
return [node.typeName.name]
2059-
case 'Record':
2059+
2060+
// TS built-in utility types
2061+
// https://www.typescriptlang.org/docs/handbook/utility-types.html
20602062
case 'Partial':
2063+
case 'Required':
20612064
case 'Readonly':
2065+
case 'Record':
20622066
case 'Pick':
20632067
case 'Omit':
2064-
case 'Required':
20652068
case 'InstanceType':
20662069
return ['Object']
20672070

2071+
case 'Uppercase':
2072+
case 'Lowercase':
2073+
case 'Capitalize':
2074+
case 'Uncapitalize':
2075+
return ['String']
2076+
2077+
case 'Parameters':
2078+
case 'ConstructorParameters':
2079+
return ['Array']
2080+
2081+
case 'NonNullable':
2082+
if (node.typeParameters && node.typeParameters.params[0]) {
2083+
return inferRuntimeType(
2084+
node.typeParameters.params[0],
2085+
declaredTypes
2086+
).filter(t => t !== 'null')
2087+
}
20682088
case 'Extract':
20692089
if (node.typeParameters && node.typeParameters.params[1]) {
20702090
return inferRuntimeType(
20712091
node.typeParameters.params[1],
20722092
declaredTypes
20732093
)
20742094
}
2075-
return ['null']
20762095
case 'Exclude':
2096+
case 'OmitThisParameter':
20772097
if (node.typeParameters && node.typeParameters.params[0]) {
20782098
return inferRuntimeType(
20792099
node.typeParameters.params[0],
20802100
declaredTypes
20812101
)
20822102
}
2083-
return ['null']
2103+
// cannot infer, fallback to null: ThisParameterType
20842104
}
20852105
}
20862106
return [`null`]

0 commit comments

Comments
 (0)