我正在寻找一种方法来强制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。
如有任何帮助,我们将不胜感激 - 非常感谢。
考虑以下:
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]