小编Zac*_*iah的帖子

我是如何获得 111 次 npm 下载的?

所以两天前我发布了我的第一个 npm 包,一个用于修剪和加入音频文件的简单库

奇怪的是我已经获得了 111 次下载。我还没有与任何人分享过这个包。我也没有记录任何东西。自述文件实际上是空白的。这可能是一个错误吗?

javascript npm npm-publish npm-install

16
推荐指数
1
解决办法
6544
查看次数

HTML 中的嵌套按钮

我正在尝试在 HTML 中显示大按钮内的按钮。

我不明白为什么 HTML 代码中的四个内部按钮显示在父按钮之外。

我在这里做错了什么?

<!DOCTYPE html>
<html> 
    <head>
        <meta charset="utf-8">
        <title>Nested Buttons</title>
        </script>
    </head>
    <body>
        <button style="height:750px; width:750px">
            Click here
            <button style="height:50px; width:50px">a</button>
            <button style="height:50px; width:50px">b</button>
            <button style="height:50px; width:50px">c</button>
            <button style="height:50px; width:50px">d</button>
        </button>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

html tags

5
推荐指数
1
解决办法
9067
查看次数

如何使用钩子更新 SolidJS 中的本地存储值

我正在尝试为solid-js 制作一个自定义“钩子”,它将从本地存储中检索状态。

import { Accessor, createSignal, Setter } from "solid-js";

export default function createLocalStorageSignal<T extends string>(key: string): [get: Accessor<T>, set: Setter<T>] {
    const storage = window.localStorage;
    const initialValue: T = JSON.parse(storage.getItem(key) ?? '{}').value;

    const [value,setValue] = createSignal<T>(initialValue);

    const newSetValue: Setter<T> = (newValue) => {
            setValue(newValue);
            storage.setItem(key, JSON.stringify({value: newValue}));

            return newValue;
        }

    return [
        value,
        newSetValue
    ]
}
Run Code Online (Sandbox Code Playgroud)

但是我收到类型错误

Type '(newValue: any) => void' is not assignable to type 'Setter<T>'
Run Code Online (Sandbox Code Playgroud)

为什么不能推断 newValue 的类型?如果无法推断,我应该将其设置为什么?

编辑: Setter<T>的完整类型是

type Setter<T> = undefined extends …
Run Code Online (Sandbox Code Playgroud)

local-storage typescript solid-js

5
推荐指数
2
解决办法
3166
查看次数

为什么打字稿不通过控制流缩小类型,而在使用箭头函数时却从不缩小类型

Typescript 拒绝通过调用来缩小范围,fail但会通过调用来缩小范围fail2。这是打字稿中的错误吗?

const fail = (message?: string): never => {
    throw new Error(message);
};

function fail2(message?: string): never {
    throw new Error(message);
}

const getData = (): string | null => {
    return "the-data";
}



export const loadDataOrError = (): string => {
    const data = getData();

    if (data === null) {
        // Swap the below and see that it works
         
        // fail2();
        fail();
    }

    // This errors
    return data;
};

Run Code Online (Sandbox Code Playgroud)

如果您想尝试切换评论并看到错误消失,这里是一个游乐场。

为了清晰起见,截图
有错误

有错误

没有错误

没有错误

typescript type-narrowing

5
推荐指数
1
解决办法
186
查看次数

由于注释而出错,这实际上不应该影响我在 python 中的程序

采取下面的代码:

"""
\u
"""
print("hello")
Run Code Online (Sandbox Code Playgroud)

该评论导致以下错误:

SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 1-2: truncated \uXXXX escape
Run Code Online (Sandbox Code Playgroud)

如果我使用#评论而不是""" ... """我不会收到此错误:

我尝试在网上搜索,但我唯一发现的是它\U实际上有一个特殊的功能。然而,在本例中,它只是一条注释,那么为什么它会影响我的程序呢? 如果我写的话"""\t"""就不会有这样的错误。

python

0
推荐指数
1
解决办法
85
查看次数