resolve(`import { X } from './foo'; defineProps<X>()`)
).toThrow(`Failed to resolve import source "./foo" for type X`)
})
+
+ test('should not error on unresolved type when inferring runtime type', () => {
+ expect(() => resolve(`defineProps<{ foo: T }>()`)).not.toThrow()
+ expect(() => resolve(`defineProps<{ foo: T['bar'] }>()`)).not.toThrow()
+ })
})
})
imports?: Record<string, ImportBinding>
scriptAst?: import('@babel/types').Statement[]
scriptSetupAst?: import('@babel/types').Statement[]
+ warnings?: string[]
+ /**
+ * Fully resolved dependency file paths (unix slashes) with imported types
+ * used in macros, used for HMR cache busting in @vitejs/plugin-vue and
+ * vue-loader.
+ */
+ deps?: string[]
}
export interface SFCStyleBlock extends SFCBlock {
import { ModelDecl } from './defineModel'
import { BindingMetadata } from '../../../compiler-core/src'
import MagicString from 'magic-string'
-import { TypeScope, WithScope } from './resolveType'
+import { TypeScope } from './resolveType'
export class ScriptCompileContext {
isJS: boolean
// codegen
bindingMetadata: BindingMetadata = {}
-
helperImports: Set<string> = new Set()
helper(key: string): string {
this.helperImports.add(key)
return `_${key}`
}
+ /**
+ * to be exposed on compiled script block for HMR cache busting
+ */
+ deps?: string[]
+
constructor(
public descriptor: SFCDescriptor,
public options: SFCScriptCompileOptions
return block.content.slice(node.start!, node.end!)
}
- error(msg: string, node: Node & WithScope, scope?: TypeScope): never {
+ error(msg: string, node: Node, scope?: TypeScope): never {
const offset = scope ? scope.offset : this.startOffset!
throw new Error(
`[@vue/compiler-sfc] ${msg}\n\n${
break
}
} else {
+ // TODO support `number` and `string` index type when possible
ctx.error(
`Unsupported index type: ${node.indexType.type}`,
node.indexType,
case 'Uncapitalize':
return getParam().map(s => s[0].toLowerCase() + s.slice(1))
default:
- ctx.error('Failed to resolve type reference', node, scope)
+ ctx.error(
+ 'Unsupported type when resolving string type',
+ node.typeName,
+ scope
+ )
}
}
}
if (node.typeName.type === 'Identifier') {
const resolved = resolveTypeReference(ctx, node, scope)
if (resolved) {
- return inferRuntimeType(ctx, resolved, scope)
+ return inferRuntimeType(ctx, resolved, resolved._ownerScope)
}
switch (node.typeName.name) {
case 'Array':
node.indexType.type === 'TSLiteralType' &&
node.indexType.literal.type === 'StringLiteral'
) {
- const resolved = resolveTypeElements(ctx, node.objectType)
- const key = node.indexType.literal.value
- return inferRuntimeType(ctx, resolved.props[key])
+ try {
+ const resolved = resolveTypeElements(ctx, node.objectType, scope)
+ const key = node.indexType.literal.value
+ const prop = resolved.props[key]
+ return inferRuntimeType(ctx, prop, prop._ownerScope)
+ } catch (e) {
+ // avoid hard error, fallback to unknown
+ return [UNKNOWN_TYPE]
+ }
}
}