如果name
是一个带有字符串";test_ word";
hWriteResult.lowest ? name === ('test_word')? hWriteResult.lowest*2 : hWriteResult.lowest
然后检查'test_word'
当我尝试使用:
hWriteResult.lowest ? name.startsWith === ('test')? hWriteResult.lowest*2 : hWriteResult.lowest
我在name.startsWith === ('test')
处得到一个错误。
(local var) name: string | undefined
Object is possibly 'undefined'.ts(2532)
This condition will always return 'false' since the types '(searchString: string, position?: number | undefined) => boolean' and 'string' have no overlap.ts(2367)
我怎样才能让它工作?
如果要测试name
值startsWtih
是否为值,请将其作为参数传递给startsWith()
函数:
name.startsWith('test')
它将返回一个布尔值。
https://www.educative.io/answers/what-is-the-startswith-method-in-typescript