-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor list tsx #1280
Refactor list tsx #1280
Conversation
赞一个,个人认为 Typescript 项目除了编译时检查以外,对 IDE 的支持意义更大。 |
8d2058c
to
17a03ce
Compare
Codecov Report
@@ Coverage Diff @@
## master #1280 +/- ##
==========================================
+ Coverage 61.75% 61.77% +0.02%
==========================================
Files 219 219
Lines 4165 4165
Branches 1225 1224 -1
==========================================
+ Hits 2572 2573 +1
+ Misses 1592 1591 -1
Partials 1 1
Continue to review full report at Codecov.
|
@cncolder SSR我帮不上什么忙,挑简单的整 😊 |
components/list/ListItem.tsx
Outdated
render() { | ||
const { | ||
styles, children, multipleLine, thumb, extra, arrow = '', style, onClick, | ||
styles = listItemStyles, children = {}, multipleLine, thumb, extra, arrow = '', style, onClick, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
设 defaultProps, 不建议解构的时候设默认值
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[ts] Object is possibly 'undefined'
不在解构的时候设置的话,typescript会报错,有什么好办法么
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://github.com/ant-design/ant-design-mobile/blob/master/components/grid/index.web.tsx#L78 目前可以这样绕过.
后续可以跟踪typescript是否会处理这个问题
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
其实想了下 [ts] Object is possibly 'undefined' 应该是对的,用户可能设置 props.styles = undefined
, 除非 typescript 结合 props interface 的类型以及 defaultProps 来推断
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
嗯 那defaultProps和解构的时候都应该设置默认值
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
一般不用,大部分逻辑下我们应该不需要拦截props.foo = undefined
这种场景吧
@@ -1,7 +1,7 @@ | |||
/* tslint:disable:no-unused-variable */ | |||
import React from 'react'; | |||
/* tslint:enable:no-unused-variable */ | |||
import List from '../list'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cncolder 这里在构建的时候是没问题的,有 webpack 配置:
resolve: {
modulesDirectories: ['node_modules', path.join(__dirname, '../node_modules')],
extensions: ['', '.web.js', '.js', '.json'],
},
typescript或者vscode有办法识别码?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
目前因为有计划2.0改后缀名 #1235
所以能不加.web的希望尽量不加
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@paranoidjk vscode 不会去识别 webpack 配置的,vscode 只认同名的 .d.ts
文件和 node_modules/@types/*
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@paranoidjk 这个后缀目前怎么说,typescript的module resolver好像没有地方可以配置
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
那就加吧
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
好嘞 现在解构的地方做了修改,估计之前也是因为ts的关系做了很多hack
17a03ce
to
7369051
Compare
components/list/ListItem.tsx
Outdated
render() { | ||
const { | ||
styles, children, multipleLine, thumb, extra, arrow = '', style, onClick, | ||
onPressIn = noop, onPressOut = noop, wrap, disabled, align, ...restProps, | ||
styles = listItemStyles, children, multipleLine, thumb, extra, arrow, style, onClick, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
有 defaultProps 就不要设解构的默认值了,太冗余。
typescript的warning另想办法处理,比如 as any,当然可以看看有没有更好方案
components/list/index.tsx
Outdated
@@ -42,14 +23,14 @@ export default class List extends React.Component<ListProps, any> { | |||
headerDom = <View>{content}</View>; | |||
} | |||
if (renderFooter) { | |||
let content = typeof renderHeader === 'function' ? renderFooter() : renderFooter; | |||
let content = typeof renderFooter === 'function' ? renderFooter() : renderFooter; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这是 bug fix 吧... cc @silentcloud
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
顺手改了...忘记分开commit了....
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
没拆开会给code review增加难度...
涉及到不同逻辑的改动还是拆开
components/checkbox/CheckboxItem.tsx
Outdated
@@ -37,7 +37,6 @@ export default class CheckboxItem extends React.Component<CheckboxItemProps, any | |||
<ListItem | |||
style={style} | |||
onClick={disabled ? undefined : this.handleClick} | |||
line={line} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里还是个多余的props..
cc @silentcloud
@bccsafe bug fix 和 逻辑的改动拆出来单独 PR,这个只做 props 的重构 |
@paranoidjk 好的 |
7369051
to
ff57d4a
Compare
解构默认值的问题,可以暂时用这个操作符规避
https://github.com/Microsoft/TypeScript/wiki/What's-new-in-TypeScript#non-null-assertion-operator
|
ping |
components/list/ListItem.tsx
Outdated
@@ -59,17 +76,17 @@ export default class Item extends React.Component<ListItemProps, any> { | |||
if (React.isValidElement(el)) { | |||
tempContentDom.push(el); | |||
} else { | |||
tempContentDom.push(<Text style={[styles.Content]} {...numberOfLines} key={`${index}-children`}>{el}</Text>); | |||
tempContentDom.push(<Text style={[styles!.Content]} {...numberOfLines} key={`${index}-children`}>{el}</Text>); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- 这个感觉代码侵入性太强了,一堆 !散落在代码里以后不好维护
- 个人感觉还不如在最前面用 as any
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as any 也得 (styles as any).Content
还不如styles!
,而且现在要去any ant-design/ant-design#5627
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
不是在每次取值的时候加 as,取值的时候做一次 https://github.com/ant-design/ant-design-mobile/blob/master/components/grid/index.web.tsx#L30
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const anyStyles = styles as any;
如果这样,之后styles的类型改了,很可能没法第一时间发现问题,但是!
不会,他只是用于确保不会null | undefined
在这次重构list的propType时,就发现了三个bug,这些bug如果声明严格,在编辑器上就能直接发现
2a0ed10
to
e29925a
Compare
ping ... |
我看看,一直在忙 😓 |
@silentcloud 也 review 下。 |
cc @pingan1927 |
@@ -0,0 +1,14 @@ | |||
import React from 'react'; | |||
import { ViewStyle } from 'react-native'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This import raises TS compile error in TS WEB projects
ERROR in /Users/aserga/www/antd-mobile-samples/web-typescript/node_modules/antd-mobile/lib/baseType.d.ts
(3,27): error TS2307: Cannot find module 'react-native'.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be fixed in #1376 @bccsafe
First of all, thanks for your contribution! :-)
Please makes sure these boxes are checked before submitting your PR, thank you!
npm run lint
and fix those errors before submitting in order to keep consistent code style.This change is![Reviewable](https://camo.githubusercontent.com/1541c4039185914e83657d3683ec25920c672c6c5c7ab4240ee7bff601adec0b/68747470733a2f2f72657669657761626c652e696f2f7265766965775f627574746f6e2e737667)
#1212