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等代替(和)
答案 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