FRONTEND_1 Telegram 3975
🔥 Юзаешь React + TypeScript? Тогда вот тебе трюк на миллион — типизация children правильно!

Часто можно увидеть вот так:


type Props = {
children: React.ReactNode;
}


Но это слишком широкий тип. Он допускает что угодно: строки, числа, фрагменты, null…

📌 Лучше — использовать ReactElement:


import { ReactElement } from 'react';

type Props = {
children: ReactElement;
}


👉 Это значит: ожидаем ровно один React-элемент, и никаких строк/чисел/массивов.

Если нужно несколько элементов — используем:


type Props = {
children: ReactElement | ReactElement[];
}


🎯 Это даёт жёсткую и предсказуемую типизацию, особенно полезно для обёрток и layout-компонентов.

⚠️ А если хочешь прям универсальность — тогда ReactNode ок. Но осознанно.


Пиши типы как профи 💪

👉 @frontend_1
👍91



tgoop.com/frontend_1/3975
Create:
Last Update:

🔥 Юзаешь React + TypeScript? Тогда вот тебе трюк на миллион — типизация children правильно!

Часто можно увидеть вот так:


type Props = {
children: React.ReactNode;
}


Но это слишком широкий тип. Он допускает что угодно: строки, числа, фрагменты, null…

📌 Лучше — использовать ReactElement:


import { ReactElement } from 'react';

type Props = {
children: ReactElement;
}


👉 Это значит: ожидаем ровно один React-элемент, и никаких строк/чисел/массивов.

Если нужно несколько элементов — используем:


type Props = {
children: ReactElement | ReactElement[];
}


🎯 Это даёт жёсткую и предсказуемую типизацию, особенно полезно для обёрток и layout-компонентов.

⚠️ А если хочешь прям универсальность — тогда ReactNode ок. Но осознанно.


Пиши типы как профи 💪

👉 @frontend_1

BY Frontend разработчик




Share with your friend now:
tgoop.com/frontend_1/3975

View MORE
Open in Telegram


Telegram News

Date: |

Telegram channels enable users to broadcast messages to multiple users simultaneously. Like on social media, users need to subscribe to your channel to get access to your content published by one or more administrators. The optimal dimension of the avatar on Telegram is 512px by 512px, and it’s recommended to use PNG format to deliver an unpixelated avatar. In handing down the sentence yesterday, deputy judge Peter Hui Shiu-keung of the district court said that even if Ng did not post the messages, he cannot shirk responsibility as the owner and administrator of such a big group for allowing these messages that incite illegal behaviors to exist. "Doxxing content is forbidden on Telegram and our moderators routinely remove such content from around the world," said a spokesman for the messaging app, Remi Vaughn. During a meeting with the president of the Supreme Electoral Court (TSE) on June 6, Telegram's Vice President Ilya Perekopsky announced the initiatives. According to the executive, Brazil is the first country in the world where Telegram is introducing the features, which could be expanded to other countries facing threats to democracy through the dissemination of false content.
from us


Telegram Frontend разработчик
FROM American