如何输入Svelte 3反应性句法变量? [英] How to type cast Svelte 3 reactive syntax variables?
本文介绍了如何输入Svelte 3反应性句法变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我不知道如何键入Svelte 3反应语法变量。
<script lang="ts">
import type { Player, Team } from "./types";
import { DEFAULT_PLAYER } from "./utils";
$: player = DEFAULT_PLAYER as Player;
$: team = { search: "Real", players: [] } as Team;
</script>
但这不起作用:
'Team' cannot be used as a value because it was imported using 'import type'.ts(1361)
如果我改用这个:
$: team = ({ search: "Real", players: [] } as Team);
VSCode扩展svelte.svelte-vscode
格式与我保存时的第一个一样。
这是我的错吗?
有没有更好的方法来转换这些被动变量?
推荐答案
<script lang="ts">
import type { Player, Team } from "./types";
import { DEFAULT_PLAYER } from "./utils";
let player: Player;
$: player = DEFAULT_PLAYER;
let team: Team;
$: team = { search: "Real", players: [] };
</script>
我不喜欢使用as
来输入CAST,并将尽可能避免这样做。使用as
进行类型强制转换可能会导致运行时类型错误,因为您告诉编译器此变量将始终是此类型,请相信我。
<script lang='ts'>
let team: Team;
$: team = { search: "Real", players: [] }
</script>
…不需要类型转换!
这篇关于如何输入Svelte 3反应性句法变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文