mirror of https://github.com/vuejs/core.git
fix(compiler-sfc): avoid all hard errors when inferring runtime type
This commit is contained in:
parent
1447596bf4
commit
2d9f6f9264
|
@ -690,6 +690,12 @@ describe('resolveType', () => {
|
||||||
test('should not error on unresolved type when inferring runtime type', () => {
|
test('should not error on unresolved type when inferring runtime type', () => {
|
||||||
expect(() => resolve(`defineProps<{ foo: T }>()`)).not.toThrow()
|
expect(() => resolve(`defineProps<{ foo: T }>()`)).not.toThrow()
|
||||||
expect(() => resolve(`defineProps<{ foo: T['bar'] }>()`)).not.toThrow()
|
expect(() => resolve(`defineProps<{ foo: T['bar'] }>()`)).not.toThrow()
|
||||||
|
expect(() =>
|
||||||
|
resolve(`
|
||||||
|
import type P from 'unknown'
|
||||||
|
defineProps<{ foo: P }>()
|
||||||
|
`)
|
||||||
|
).not.toThrow()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
|
@ -1180,6 +1180,7 @@ export function inferRuntimeType(
|
||||||
node: Node & MaybeWithScope,
|
node: Node & MaybeWithScope,
|
||||||
scope = node._ownerScope || ctxToScope(ctx)
|
scope = node._ownerScope || ctxToScope(ctx)
|
||||||
): string[] {
|
): string[] {
|
||||||
|
try {
|
||||||
switch (node.type) {
|
switch (node.type) {
|
||||||
case 'TSStringKeyword':
|
case 'TSStringKeyword':
|
||||||
return ['String']
|
return ['String']
|
||||||
|
@ -1211,7 +1212,11 @@ export function inferRuntimeType(
|
||||||
}
|
}
|
||||||
case 'TSPropertySignature':
|
case 'TSPropertySignature':
|
||||||
if (node.typeAnnotation) {
|
if (node.typeAnnotation) {
|
||||||
return inferRuntimeType(ctx, node.typeAnnotation.typeAnnotation, scope)
|
return inferRuntimeType(
|
||||||
|
ctx,
|
||||||
|
node.typeAnnotation.typeAnnotation,
|
||||||
|
scope
|
||||||
|
)
|
||||||
}
|
}
|
||||||
case 'TSMethodSignature':
|
case 'TSMethodSignature':
|
||||||
case 'TSFunctionType':
|
case 'TSFunctionType':
|
||||||
|
@ -1284,13 +1289,21 @@ export function inferRuntimeType(
|
||||||
break
|
break
|
||||||
case 'Extract':
|
case 'Extract':
|
||||||
if (node.typeParameters && node.typeParameters.params[1]) {
|
if (node.typeParameters && node.typeParameters.params[1]) {
|
||||||
return inferRuntimeType(ctx, node.typeParameters.params[1], scope)
|
return inferRuntimeType(
|
||||||
|
ctx,
|
||||||
|
node.typeParameters.params[1],
|
||||||
|
scope
|
||||||
|
)
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
case 'Exclude':
|
case 'Exclude':
|
||||||
case 'OmitThisParameter':
|
case 'OmitThisParameter':
|
||||||
if (node.typeParameters && node.typeParameters.params[0]) {
|
if (node.typeParameters && node.typeParameters.params[0]) {
|
||||||
return inferRuntimeType(ctx, node.typeParameters.params[0], scope)
|
return inferRuntimeType(
|
||||||
|
ctx,
|
||||||
|
node.typeParameters.params[0],
|
||||||
|
scope
|
||||||
|
)
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
@ -1317,19 +1330,14 @@ export function inferRuntimeType(
|
||||||
return ['Symbol']
|
return ['Symbol']
|
||||||
|
|
||||||
case 'TSIndexedAccessType': {
|
case 'TSIndexedAccessType': {
|
||||||
try {
|
|
||||||
const types = resolveIndexType(ctx, node, scope)
|
const types = resolveIndexType(ctx, node, scope)
|
||||||
return flattenTypes(ctx, types, scope)
|
return flattenTypes(ctx, types, scope)
|
||||||
} catch (e) {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
case 'ClassDeclaration':
|
case 'ClassDeclaration':
|
||||||
return ['Object']
|
return ['Object']
|
||||||
|
|
||||||
case 'TSImportType': {
|
case 'TSImportType': {
|
||||||
try {
|
|
||||||
const sourceScope = importSourceToScope(
|
const sourceScope = importSourceToScope(
|
||||||
ctx,
|
ctx,
|
||||||
node.argument,
|
node.argument,
|
||||||
|
@ -1340,7 +1348,6 @@ export function inferRuntimeType(
|
||||||
if (resolved) {
|
if (resolved) {
|
||||||
return inferRuntimeType(ctx, resolved, resolved._ownerScope)
|
return inferRuntimeType(ctx, resolved, resolved._ownerScope)
|
||||||
}
|
}
|
||||||
} catch (e) {}
|
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1356,6 +1363,9 @@ export function inferRuntimeType(
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} catch (e) {
|
||||||
|
// always soft fail on failed runtime type inference
|
||||||
|
}
|
||||||
return [UNKNOWN_TYPE] // no runtime check
|
return [UNKNOWN_TYPE] // no runtime check
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue