小编Tri*_*sne的帖子

TypeScript:在 JSON 中序列化 BigInt

我正在寻找一种方法来强制JSON.stringify始终打印BigInts 而不会抱怨。

我知道它是非标准的,我知道有一个纯 JavaScript 的包;但它不符合我的需求。我什至知道通过设置 来修复原始 JavaScript BigInt.prototype.toJSON。我需要的是某种方法来JSON.stringify在我的 TypeScript 代码中全局覆盖正常函数。

大约一年前,我发现了以下代码:

declare global
{
    interface BigIntConstructor
    {
        toJSON:()=>BigInt;
    }
}

BigInt.toJSON = function() { return this.toString(); };
Run Code Online (Sandbox Code Playgroud)

在某些网页上我无法再次找到。它曾经在我的另一个项目中工作过,但现在似乎不再工作了。我不知道为什么。

无论我对上面的行做什么,如果我尝试打印包含 BigInt 的 JSON,我会得到:TypeError: Do not know how to serialize a BigInt

如有任何帮助,我们将不胜感激 - 非常感谢。

json global bigint typescript

15
推荐指数
2
解决办法
2万
查看次数

Python:对于继承的类,类型提示返回类实例的类方法

考虑以下:

from __future__ import annotations

class A:

    def __init__(self):
        print("A")
        self.hello = "hello"

    # how do I type this so that the return type is A for A.bobo()
    # and B for B.bobo()?
    @classmethod
    def bobo(cls) -> UnknownType:
        return cls()


class B(A):

    def __init__(self):
        print("B")
        super().__init__()
        self.world = "world"


instance_of_B = B.bobo()  # prints "B", then "A", and returns an instance of B
Run Code Online (Sandbox Code Playgroud)

我想对类方法进行类型提示,以便 mypy 可以知道,在s方法bobo的情况下,它不仅仅是返回的实例,而且实际上是 的实例。我真的不清楚如何做到这一点,或者是否可能。我认为类似的东西可能会起作用,但我不确定这对 mypy 是否具有语法意义。BboboABType[cls]

python typing mypy

11
推荐指数
2
解决办法
8938
查看次数

标签 统计

bigint ×1

global ×1

json ×1

mypy ×1

python ×1

typescript ×1

typing ×1