react-component / react-component/util

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

Open
#423 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
670
Forks
205
Avg merge
11d 17h
Merged PRs (30d)
4

Description

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时发现: https://github.com/ant-design/pro-components/issues/6652

个人见解,欢迎讨论😁

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the repository's tsconfig and the useMergedState.ts example, especially the generic declaration near line 19 and the effect beginning around line 60. Consider the linked pro-components issue for context, then determine whether enabling strict or strictNullChecks is appropriate and define the resulting type-safety acceptance criteria.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
build-system
Issue type
Refactor
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.