Vue 3更新后,withDefaults会引发TypeScript错误。

huangapple go评论73阅读模式
英文:

After update of Vue 3 withDefaults throws a TypeScript error

问题

I'm providing the translation for the text you provided:

我试图更新 Vue 3 的版本(从 3.2.x3.3.x)。

但出现了一个问题,当我使用 withDefaults 时会出现错误:

错误消息是:

> 参数类型 DefineProps<Props, BooleanKey<Props>> 无法赋值给参数类型 DefineProps<Readonly<Props> & {}, keyof Readonly<Props> & {}>

我猜我是按照文档中描述的方式做的,问题出在哪里?

更新:看起来这个问题只会发生在非布尔字段上:


// 这个可以工作
withDefaults(defineProps<{readonly foo: boolean}>(), { foo: true });


// 这个不工作
withDefaults(defineProps<{readonly foo: string}>(), { foo: 'foo' });
英文:

I'm trying to update version of Vue 3 (3.2.x -> 3.3.x).

But for some reason I have an error when using withDefaults:

<script setup lang="ts">
interface Props {
  readonly title: string;
}

// THIS WORKS
const props = defineProps<Props>(); 

// THIS DOESN'T WORK (guess because of withDefaults)
const props = withDefaults(defineProps<Props>(), { title: '' });
...
</script>

The error message is:

>Argument type DefineProps<Props, BooleanKey<Props>> is not assignable to parameter type DefineProps<Readonly<Props> & {}, keyof Readonly<Props> & {}>

I guess I'm doing this exactly how it's described in docs, what's the problem here?


Update: Looks like this issue happens only for non-boolean fields:


// THIS WORKS
withDefaults(defineProps<{readonly foo: boolean}>(), { foo: true });


// THIS DOESN'T WORK
withDefaults(defineProps<{readonly foo: string}>(), { foo: 'foo' });

答案1

得分: 5

这是IDE的错误,已在WEB-61241上进行跟踪;请关注它以获取更新。

作为一种临时解决方法,您可以尝试下载WebStorm 2023.2 EAP,并在首选项 | 语言和框架 | TypeScript | Vue中启用Volar支持。

英文:

This is the IDE bug, tracked at WEB-61241; please follow it for updates

as a workaround, you can try downloading the WebStorm 2023.2 EAP and enabling Volar support in Preferences | Languages & Frameworks | TypeScript | Vue

huangapple
  • 本文由 发表于 2023年5月24日 18:35:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76322580.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定