Browse Source

docs: update faq about strictNullChecks (#43332)

* docs: update faq about strictNullChecks

* docs: update faq about strictNullChecks
pull/43297/merge
kiner-tang(文辉) 1 year ago
committed by GitHub
parent
commit
873afe1e0b
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 31
      docs/react/faq.en-US.md
  2. 31
      docs/react/faq.zh-CN.md

31
docs/react/faq.en-US.md

@ -234,3 +234,34 @@ Here are some typical wrong examples:
## Do you guys have any channel or website for submitting monetary donations, like through PayPal or Alipay? ## Do you guys have any channel or website for submitting monetary donations, like through PayPal or Alipay?
[https://opencollective.com/ant-design](https://opencollective.com/ant-design) [https://opencollective.com/ant-design](https://opencollective.com/ant-design)
## Use Form's `setFieldsValue` method to report an error if the object type contains `null`
When we try to set the form value using the `setFieldsValue` method in the form instance of the form component, if the passed object contains the type null, such as:
```tsx
// This is not real world code, just for explain
import { Form } from 'antd';
type Test = {
value: string[] | null;
};
export default () => {
const [form] = Form.useForm<Test>();
form.setFieldsValue({
value: null, // Error: Type "null" cannot be assigned to type "string[] | undefined".
});
};
```
If you encounter the above error, please check the current project `tsconfig.json` contains the following configuration:
```json
{
"strictNullChecks": true
}
```
The above problem occurs if `strictNullChecks` is set to `true`, If you can determine the project don't need this configuration (see [strictNullChecks](https://www.typescriptlang.org/zh/tsconfig#strictNullChecks) to judge whether need the configuration). You can try changing to `false` to turn off the control strict check. However, if you do need to enable this feature, you can avoid this situation by using other types instead of `null` when designing types

31
docs/react/faq.zh-CN.md

@ -256,3 +256,34 @@ import { ConfigProvider } from 'antd';
## 你们有接受捐助的渠道吗,比如支付宝或者微信支付? ## 你们有接受捐助的渠道吗,比如支付宝或者微信支付?
[https://opencollective.com/ant-design](https://opencollective.com/ant-design) [https://opencollective.com/ant-design](https://opencollective.com/ant-design)
## 使用表单组件的 `setFieldsValue` 方法如果对象类型中含有 `null` 时 TS 类型报错
当我们尝试使用表单组件的表单实例当中的 `setFieldsValue` 方法设置表单值时,如果在传入的对象中包含有 `null` 类型,如:
```tsx
// This is not real world code, just for explain
import { Form } from 'antd';
type Test = {
value: string[] | null;
};
export default () => {
const [form] = Form.useForm<Test>();
form.setFieldsValue({
value: null, // Error: 不能将类型“null”分配给类型“string[] | undefined”。
});
};
```
如果你遇到上述报错,请检查当前项目的 `tsconfig.json` 中是否包含如下配置:
```json
{
"strictNullChecks": true
}
```
如果 `strictNullChecks` 的值被设置为 `true` 就会出现上述问题,如果你确定项目中可以不需要这个检测配置(查看[strictNullChecks](https://www.typescriptlang.org/zh/tsconfig#strictNullChecks)判断是否需要该配置),可以尝试改为 `false` 关闭控制严格检查功能。但如果你确实需要开启这个功能,那么,你可以在设计类型时,使用其他类型替代 `null` 以避免出现这种情况。

Loading…
Cancel
Save