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 b71f801
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 11 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 }
declare const obj: Bar
declare const set: Set<any>
declare const arr: Array<any>
defineProps<{
imp: keyof IMP,
foo: keyof Foo,
bar: keyof Bar,
obj: keyof typeof obj,
set: keyof typeof set,
arr: keyof typeof arr
}>()
`,
files,
)

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

test('utility type: ReadonlyArray', () => {
expect(
resolve(`
Expand Down
57 changes: 46 additions & 11 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,33 @@ export function inferRuntimeType(
const types = new Set<string>()
const members =
node.type === 'TSTypeLiteral' ? node.members : node.body.body

const handler = isKeyof
? (m: TSTypeElement) => {
if (
m.type === 'TSPropertySignature' &&
m.key.type === 'NumericLiteral'
) {
types.add('Number')
} else {
types.add('String')
}
}
: (m: TSTypeElement) => {
if (
m.type === 'TSCallSignatureDeclaration' ||
m.type === 'TSConstructSignatureDeclaration'
) {
types.add('Function')
} else {
types.add('Object')
}
}

for (const m of members) {
if (
m.type === 'TSCallSignatureDeclaration' ||
m.type === 'TSConstructSignatureDeclaration'
) {
types.add('Function')
} else {
types.add('Object')
}
handler(m)
}

return types.size ? Array.from(types) : ['Object']
}
case 'TSPropertySignature':
Expand Down Expand Up @@ -1512,9 +1532,22 @@ 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 (node.typeName.type === 'Identifier') {
if (isKeyof) {
switch (node.typeName.name) {
case 'String':
case 'Array':
case 'ArrayLike':
case 'ReadonlyArray':
return ['String', 'Number']
default:
return ['String']
}
}

switch (node.typeName.name) {
case 'Array':
case 'Function':
Expand Down Expand Up @@ -1634,15 +1667,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 b71f801

Please sign in to comment.