使用参考时,打字稿省略不显示错误

时间:2018-12-15 18:53:04

标签: typescript typescript-types

test_func()

我期望这会有一些智能错误,但事实并非如此。但是,这确实显示出一些错误,有人知道为什么吗?

ndimage.generic_filter()

2 个答案:

答案 0 :(得分:2)

通过设计,将对象定义为文字时,TS仅在多余属性上出错。 https://www.typescriptlang.org/docs/handbook/interfaces.html#excess-property-checks

答案 1 :(得分:2)

除了@kingdaro关于对象文字的多余属性检查的正确观点之外,

class Char: name = "undefined" def __init__(self, race, str, int, dex, con, spd, mp_bonus): self.race = race self.exp = 0 self.lvl = 1 self.str = str self.int = int self.dex = dex self.con = con self.spd = spd self.hp = (con + str) / 2 self.current_hp = self.hp self.mp_bonus = mp_bonus self.mp = (int * mp_bonus) self.current_mp = self.mp def save(self): with open("save.pk1", "wb") as fp: pickle.dump(self.__dict__, fp, protocol=pickle.HIGHEST_PROTOCOL) def load(): with open('save.pk1', 'rb') as fp: Char.__init__ = pickle.load(fp) # no idea what to put here # or if it should be in the Char class or not def options(dude): cls() print("OPTIONS") print("_____________________") print("s. Save Game") print("l. Load Game") print("x. Quit Game") print("_____________________") select = input("please type in the corresponding letter or number: ") if select == "s": Char.save(player) cls() print("Save Complete") wait() main(dude) elif select == "l": cls() print("Load Complete") wait() main(dude) elif select == "x": exit_screen(dude) else: print("you chose the wrong key") wait() main(dude) def main(dude): #menu as written in options above select = input("please type in the corresponding letter or number: ") if select == "s": stats(dude) elif select == "i": inventory(dude) elif select == "1": rand_enemy() elif select == "o": options(dude) else: print("you chose the wrong key") wait() main(dude) def start_screen(char): #menu as written in options above select = input("Please type in the corresponding number: ") if select == "1": get_char(char) elif select == "2": load() main(char) elif select == "3": exit() else: print("you chose the wrong key") wait() start_screen(char) start_screen(Char) 的定义只是扩大了Omit<T,K>的类型,因此它在T处不包含 known 属性(“我不知道或关心K是否是一个属性;我将忽略它。”),这与在K禁止一个属性不同。也就是说,KT的子类型,并且类型Omit<T, K>的任何值也可以分配给类型T。如果您的意图实际上是在Omit<T, K>禁止一个属性,则可以(通过某种方式)通过指定K属性为可选属性并将其值类型设置为{ {1}}(或K,请参见下文)。我们称之为never

undefined

让我们看看它是否有效:

ForcefullyOmit<T, K>

看起来不错。

这只是禁止密钥的“一种”,因为它仍将允许类型为type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>; type ForcefullyOmit<T, K extends keyof T> = Omit<T, K> & Partial<Record<K, never>>; (或interface ObjInterface { first: string; second: string; } const obj = { first: "first", second: "second" }; const out: ForcefullyOmit<ObjInterface, "first"> = obj; // error // types of property "first" are incompatible // string is not assignable to undefined )的那个密钥的属性,但这有点像unresolved issue TypeScript ...语言并不总是能区分缺少的属性和undefined的属性。

由您决定用例是否确实需要更严格的行为never。无论如何,希望能有所帮助;祝你好运!