umi-examples icon indicating copy to clipboard operation
umi-examples copied to clipboard

空值检查

Open yuri-li opened this issue 6 years ago • 4 comments

我希望umi可以对null值做严格的测试(不要给对象赋值null!编译的时候就报错)。demo如下:

  1. 测试代码:
it("test null check", () => {
    let name:string = null
    expect(name).toBe(null)
})
  1. tsconfig.json "strictNullChecks": true
  2. test命令 umi test

yuri-li avatar Jun 02 '19 03:06 yuri-li

没明白问题,这不是自己配 tsconfig.json 能解决的吗?

sorrycc avatar Jun 03 '19 02:06 sorrycc

没明白问题,这不是自己配 tsconfig.json 能解决的吗?

我贴的是测试代码,没有达到目的。你怎么配置的?

yuri-li avatar Jun 05 '19 09:06 yuri-li

"strict": true,

xiaohuoni avatar Jun 05 '19 09:06 xiaohuoni

"strict": true,

为了让结果更明显,我改了测试代码,如下:

function allowNull(name?:string):boolean{
    return name == null
}
function notAllowNull(name:string):boolean{
    return name == null
}
it("check null value", () => {
    let name:string = null
    
    expect(allowNull(name)).toBeTruthy()

    expect(notAllowNull(name)).toBeTruthy()
})

null-check

yuri-li avatar Jun 05 '19 12:06 yuri-li