错误TS2365:操作员'!=='不适用于类型""("'和'")"'

时间:2017-06-05 10:45:00

标签: typescript

function myFunc(id: string) {
  id = "(";
};

let id: string = ")";
if (id !== '(')
  throw "Expected '(' in prototype";

myFunc(id);

if (id !== ')')
  throw "Expected ')' in prototype";

如果我进行内联更改,那么我不会得到以下错误

error TS2365: Operator '!==' cannot be applied to types '"("' and '")"'.

比较的内容无关紧要。可以使用任何字符串a,basdasd,cqwe等代替(和)

1 个答案:

答案 0 :(得分:1)

类型缩小会发生什么。传递第一个if语句后,MORSE_CHARS = { "a": (dot, line), "b": (dot, dot, line), # define other characters and space here } def morse(s): """ Turn string s into Morse beeps """ for ch in s.lower(): for fn in MORSE_CHARS[ch]: fn() def main(): while True: s = input("Enter text to play (or just hit Enter to quit): ") if s: morse(s) else: break if __name__ == "__main__": main() 的类型缩小为字符串文字")"因为否则会抛出异常。

id