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(compile-sfc): handle mapped types work with omit and pick #12648

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading