【发布时间】:2022-01-05 15:21:34
【问题描述】:
好吧可能是一个简单的,但这让我很困惑
我有一个使用定义的 SFC
export default defineComponent({
setup(): { args: Args; preview: Point[] } {
const args = new Args();
const preview: Point[] = [];
args.values.push(...args.shape.shapeArgs.map((d) => d.default));
return { args, preview: preview };
},
computed: {
previewSize() {
return {
width: (Math.max(1, ...this.preview.map((p) => p.x)) + 40) * 10,
height: (Math.max(1, ...this.preview.map((p) => p.y)) + 40) * 10,
};
},
...
但是它不会运行,因为
TS2339: Property 'preview' does not exist on type 'ComponentPublicInstance<{} | {}, {}, {}, {}, {}, EmitsOptions, {} | {}, {}, false, ComponentOptionsBase<{} | {}, {}, {}, {}, {}, ComponentOptionsMixin, ComponentOptionsMixin, EmitsOptions, string, {}>>'.
Property 'preview' does not exist on type '{ $: ComponentInternalInstance; $data: {}; $props: {} | {}; $attrs: Record<string, unknown>; $refs: Record<string, unknown>; $slots: Readonly<InternalSlots>; ... 7 more ...; $watch(source: string | Function, cb: Function, options?: WatchOptions<...> | undefined): WatchStopHandle; } & ... 4 more ... & ComponentCustom...'.
56 | previewSize() {
57 | return {
> 58 | width: (Math.max(1, ...this.preview.map((p) => p.x)) + 40) * 10,
| ^^^^^^^
由于明确定义了预览,我对它为什么不存在感到困惑
【问题讨论】:
-
这很有趣,因为实际上我发现的每个示例都使用类型推断来进行打字,而没有任何明确的内容,因此它显然应该能够捡起它
-
感谢@Francesco Vattiato,看来我的思路已经交叉,并且以不应该混合的方式将选项和构图混合在一起
标签: javascript typescript vue.js