代码之家  ›  专栏  ›  技术社区  ›  Fred Hors

为什么typescript无法检测到这个苗条3商店的正确内容类型,而告诉我它是任何类型的?

  •  0
  • Fred Hors  · 技术社区  · 5 年前

    使用此代码和 eslint-plugin-svelte3 :

    <script lang="ts">
        import { writable } from "svelte/store";
    
        type Player = {
            name: string;
        };
    
        const playersStore = writable([] as Player[]);
    
        $: players = $playersStore;
    </script>
    
    {players}
    

    我发现这个打字错误:

    Unsafe assignment of an any value.eslint@typescript-eslint/no-unsafe-assignment

    在线:

    $: players = $playersStore;
    

    为什么?

    如果我使用以下命令,错误就会消失:

    $: players = playersStore;
    

    但我认为这不是一个解决方案:这是一个错误,对吗?

    0 回复  |  直到 5 年前
        1
  •  1
  •   dummdidumm    5 年前

    From the GitHub readme :

    目前,这些类型感知规则存在一些限制。具体来说,在反应性分配和商店订阅的上下文中进行的检查将根据规则报告误报或漏报。在被动赋值的情况下,可以通过显式键入被动变量来解决这个问题。

    我的建议是 turn off the offending rule 在这一限制得到解决之前,为苗条的文件。

    推荐文章