Skip to content

[RFC] tsconfig 是否应该开启 strict 或 strictNullChecks #423

Open
@xliez

Description

@xliez

WHY:不开启strictNullChecks的话会忽略所有的 null 和 undefined

举个例子:

useMergedState 中接受了泛型T作为内部的 innerValue 类型,但根据 L60 开始的代码

  // Sync value back to `undefined` when it from control to un-control
  useLayoutUpdateEffect(() => {
    if (!hasValue(value)) {
      setInnerValue(value);
    }
  }, [value]);

实际上 innerValue 的类型是T | undefined,因为没有开启 strictNullChecks 导致没有警告

该函数使用中,需要显性去指定泛型为 T | undefined 才能获取到实际正确的类型,如:

const [v] = useMergedState<string[]>(...) => const [v] = useMergedState<string[] | undefined>(...)

前者的写法,v的类型会忽略 undefined类型,导致后续编码产生错误

以上为我在debug该issue时发现: ant-design/pro-components#6652

个人见解,欢迎讨论😁

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions