Skip to content

Commit

Permalink
fix(compiler-sfc): handle keyof operator
Browse files Browse the repository at this point in the history
  • Loading branch information
jh-leong committed May 6, 2024
1 parent c0c9432 commit e77c0e3
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 9 deletions.
36 changes: 36 additions & 0 deletions packages/compiler-sfc/__tests__/compileScript/resolveType.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,42 @@ describe('resolveType', () => {
})
})

test('utility type: keyof', () => {
const files = {
'/foo.ts': `export type IMP = { ${1}: 1 };`,
}

const { props } = resolve(
`
import { IMP } from './foo'
interface Foo { foo: 1, ${1}: 1 }
type Bar = { bar: 1 }
type SubBar = Bar
declare const obj: Bar
declare const set: Set<any>
defineProps<{
imp: keyof IMP,
foo: keyof Foo,
bar: keyof Bar,
subBar: keyof SubBar,
obj: keyof typeof obj,
set: keyof typeof set,
}>()
`,
files,
)

expect(props).toStrictEqual({
imp: ['Number'],
foo: ['String', 'Number'],
bar: ['String'],
subBar: ['String'],
obj: ['String'],
set: ['String'],
})
})

test('utility type: ReadonlyArray', () => {
expect(
resolve(`
Expand Down
36 changes: 27 additions & 9 deletions packages/compiler-sfc/src/script/resolveType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1448,7 +1448,10 @@ export function inferRuntimeType(
ctx: TypeResolveContext,
node: Node & MaybeWithScope,
scope = node._ownerScope || ctxToScope(ctx),
options: { isKeyof?: boolean } = {},
): string[] {
const { isKeyof } = options

try {
switch (node.type) {
case 'TSStringKeyword':
Expand All @@ -1467,16 +1470,26 @@ export function inferRuntimeType(
const types = new Set<string>()
const members =
node.type === 'TSTypeLiteral' ? node.members : node.body.body

for (const m of members) {
if (
m.type === 'TSCallSignatureDeclaration' ||
m.type === 'TSConstructSignatureDeclaration'
) {
types.add('Function')
if (isKeyof) {
if ((m as any).key.type === 'NumericLiteral') {
types.add('Number')
} else {
types.add('String')
}
} else {
types.add('Object')
if (
m.type === 'TSCallSignatureDeclaration' ||
m.type === 'TSConstructSignatureDeclaration'
) {
types.add('Function')
} else {
types.add('Object')
}
}
}

return types.size ? Array.from(types) : ['Object']
}
case 'TSPropertySignature':
Expand Down Expand Up @@ -1512,8 +1525,11 @@ export function inferRuntimeType(
case 'TSTypeReference': {
const resolved = resolveTypeReference(ctx, node, scope)
if (resolved) {
return inferRuntimeType(ctx, resolved, resolved._ownerScope)
return inferRuntimeType(ctx, resolved, resolved._ownerScope, options)
}

if (isKeyof) return ['String']

if (node.typeName.type === 'Identifier') {
switch (node.typeName.name) {
case 'Array':
Expand Down Expand Up @@ -1634,15 +1650,17 @@ export function inferRuntimeType(
// typeof only support identifier in local scope
const matched = scope.declares[id.name]
if (matched) {
return inferRuntimeType(ctx, matched, matched._ownerScope)
return inferRuntimeType(ctx, matched, matched._ownerScope, options)
}
}
break
}

// e.g. readonly
case 'TSTypeOperator': {
return inferRuntimeType(ctx, node.typeAnnotation, scope)
return inferRuntimeType(ctx, node.typeAnnotation, scope, {
isKeyof: node.operator === 'keyof',
})
}
}
} catch (e) {
Expand Down

0 comments on commit e77c0e3

Please sign in to comment.