Skip to content

Commit

Permalink
fix(compile-sfc): handle mapped types work with omit and pick
Browse files Browse the repository at this point in the history
  • Loading branch information
edison1105 committed Jan 6, 2025
1 parent e8e8422 commit b8f8f8b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
17 changes: 17 additions & 0 deletions packages/compiler-sfc/__tests__/compileScript/resolveType.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,23 @@ describe('resolveType', () => {
})
})

test('utility type: mapped type with Omit and Pick', () => {
expect(
resolve(`
type Optional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>
interface Test {
foo: string;
bar?: string;
}
type OptionalTest = Optional<Test, 'foo'>
defineProps<OptionalTest>()
`).props,
).toStrictEqual({
foo: ['String'],
bar: ['String'],
})
})

test('utility type: ReadonlyArray', () => {
expect(
resolve(`
Expand Down
22 changes: 18 additions & 4 deletions packages/compiler-sfc/src/script/resolveType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -546,26 +546,38 @@ function resolveStringType(
ctx: TypeResolveContext,
node: Node,
scope: TypeScope,
typeParameters?: Record<string, Node>,
): string[] {
switch (node.type) {
case 'StringLiteral':
return [node.value]
case 'TSLiteralType':
return resolveStringType(ctx, node.literal, scope)
return resolveStringType(ctx, node.literal, scope, typeParameters)
case 'TSUnionType':
return node.types.map(t => resolveStringType(ctx, t, scope)).flat()
return node.types
.map(t => resolveStringType(ctx, t, scope, typeParameters))
.flat()
case 'TemplateLiteral': {
return resolveTemplateKeys(ctx, node, scope)
}
case 'TSTypeReference': {
const resolved = resolveTypeReference(ctx, node, scope)
if (resolved) {
return resolveStringType(ctx, resolved, scope)
return resolveStringType(ctx, resolved, scope, typeParameters)
}
if (node.typeName.type === 'Identifier') {
const name = node.typeName.name
if (typeParameters && typeParameters[name]) {
return resolveStringType(
ctx,
typeParameters[name],
scope,
typeParameters,
)
}
const getParam = (index = 0) =>
resolveStringType(ctx, node.typeParameters!.params[index], scope)
switch (node.typeName.name) {
switch (name) {
case 'Extract':
return getParam(1)
case 'Exclude': {
Expand Down Expand Up @@ -671,6 +683,7 @@ function resolveBuiltin(
ctx,
node.typeParameters!.params[1],
scope,
typeParameters,
)
const res: ResolvedElements = { props: {}, calls: t.calls }
for (const key of picked) {
Expand All @@ -683,6 +696,7 @@ function resolveBuiltin(
ctx,
node.typeParameters!.params[1],
scope,
typeParameters,
)
const res: ResolvedElements = { props: {}, calls: t.calls }
for (const key in t.props) {
Expand Down

0 comments on commit b8f8f8b

Please sign in to comment.