#reactjs #typescript #react-native #eslint #tsdoc
#reactjs #typescript #react-native #eslint #tsdoc
Вопрос:
Я использую JSDoc и TSDoc для проекта React Native с TypeScript. При документировании реквизитов наблюдается некоторое странное поведение.
Все @param: props.propName подчеркнуты сообщением:
tsdoc-param-tag-with-invalid-name: The @param block should be followed by a valid parameter name: The identifier cannot non-word characterseslinttsdoc/syntax
Кроме того, я обязан добавить : Props дважды, потому что, если я помещаю его только в FC, реквизиты подчеркиваются:
'onPress' is missing in props validationeslintreact/prop-types
Код:
import React, { useContext, FC } from 'react'
import { GestureResponderEvent, ViewStyle } from 'react-native'
import { useNavigation } from '@react-navigation/native'
import { UserAvatarContext } from '../apps'
import Avatar from './Avatar'
interface Props {
size?: number
radius?: number
style?: ViewStyle
onPress?: (event: GestureResponderEvent) => void
}
/**
* Display the user profile avatar and link
*
* @param props - React props
* @param props.size - the size of the avatar in pixels
* @param props.radius - the border radius in pixels
* @param props.onPress - the function to use when pressing the avatar (by default, navigate to the user profile page)
* @param props.style - Additional style information
* @returns The avatar icon
*/
const UserAvatar: FC<Props> = ({ size = 40, radius, style, onPress }: Props) => {
const navigation = useNavigation()
const { source } = useContext(UserAvatarContext)
const defaultOnPress = (): void => navigation.navigate('My profile')
return <Avatar source={source} onPress={onPress || defaultOnPress} size={size} style={style} radius={radius} />
}
export default UserAvatar
Я бы хотел, чтобы он был чистым, но я чувствую, что мне нужно внести некоторые конфигурации или изменить способ объявления моего реквизита. Есть идеи?
Спасибо
Ответ №1:
Просто переместите описание свойства в определение интерфейса, как показано ниже:
import React, { useContext, FC } from 'react'
import { GestureResponderEvent, ViewStyle } from 'react-native'
import { useNavigation } from '@react-navigation/native'
import { UserAvatarContext } from '../apps'
import Avatar from './Avatar'
interface Props {
// the size of the avatar in pixels
size?: number
// the border radius in pixels
radius?: number
// Additional style information
style?: ViewStyle
// the function to use when pressing the avatar (by default, navigate to the user profile page)
onPress?: (event: GestureResponderEvent) => void
}
/**
* Display the user profile avatar and link
*
* @param props - React props
*/
const UserAvatar: FC<Props> = ({ size = 40, radius, style, onPress }: Props) => {
const navigation = useNavigation()
const { source } = useContext(UserAvatarContext)
const defaultOnPress = (): void => navigation.navigate('My profile')
return <Avatar source={source} onPress={onPress || defaultOnPress} size={size} style={style} radius={radius} />
}