Zum Hauptinhalt springen
Echtzeit-Radar & Feeds
Alle RSS Feeds ➔
👥 Community & Social
•
Sichere ProgrammierungCheap bloom for a Canvas 2D game in 8 lines (no WebGL)(29.09.2026 um 00:27 Uhr)
•••
Sichere ProgrammierungThe cheapest S3 class is the one you never open(29.09.2026 um 00:32 Uhr)
•
Sichere ProgrammierungFlash Loan Attack Vector Analysis: USDT0(29.09.2026 um 00:34 Uhr)
•
Sichere ProgrammierungThe complete anatomy of a Stripe Connect refund(29.09.2026 um 00:34 Uhr)
•
Sicherheitslücken (CVE)Introduction to Network-Based Exploit Development(29.09.2026 um 00:35 Uhr)
•
Sichere ProgrammierungA classe mais barata do S3 é a que você nunca abre(29.09.2026 um 00:36 Uhr)
•••
Sichere ProgrammierungCheap bloom for a Canvas 2D game in 8 lines (no WebGL)(29.09.2026 um 00:27 Uhr)
•••
Sichere ProgrammierungThe cheapest S3 class is the one you never open(29.09.2026 um 00:32 Uhr)
•
Sichere ProgrammierungFlash Loan Attack Vector Analysis: USDT0(29.09.2026 um 00:34 Uhr)
•
Sichere ProgrammierungThe complete anatomy of a Stripe Connect refund(29.09.2026 um 00:34 Uhr)
•
Sicherheitslücken (CVE)Introduction to Network-Based Exploit Development(29.09.2026 um 00:35 Uhr)
•
Sichere ProgrammierungA classe mais barata do S3 é a que você nunca abre(29.09.2026 um 00:36 Uhr)
••
Intelligence View
⚡ tsecurity.de Intelligence

Advanced typings

Lets assume you're trying to create a generic component that receives a data-fetching function, lets call it fn, which returns an item or an array of some type and another prop, lets call it key, with one of the keys of the function's…

0
↗ Quelle (dev.to)
Reagiere als Erste:r — dein Feedback zählt!

Lets assume you're trying to create a generic component that receives a data-fetching function, lets call it fn, which returns an item or an array of some type and another prop, lets call it key, with one of the keys of the function's return type.



In order to get autocompletion for the key prop.

Auto completion





The data-fetching function





async function getData() {
return {key1: "b", key2: 4, key3: false};
}







The Question



How to declare the type of another component's prop to accept only the keys of the data type returned by the data-fetching function.





The Process



You could "easily" type the props as follows:




type ReturnedType = {
key1: string
key2: number
key3: boolean
}
type ComponentProps = {
fn: () => Promise<ReturnedType>
key: "key1" | "key2" | "key3"
}






But this doesn't really scale as the component is not generic, meaning the data-fetching function can only be one that returns an object with the shape of ReturnedType.



Also, every time the type ReturnedType changes, we need to make changes to the type of the key prop.




Note

We could also declare the key's prop type as keyof ReturnedType.







The Fix: Inferring



Then we can define our component's type and use keyof to define the type of key prop resulting in the type being the keys of the awaited response of the function we passed on fn.



TL;DR:




type ComponentType = <
F extends () => Promise<Record<string, unknown>>,
D extends Awaited<ReturnType<F>>
> ({
fn,
key
}: {
fn: F,
key: keyof D
}) => React.JSX.Element | null






In this case we need to check for the type of key prop to be "string":




// Server component
const Component: ComponentType = async ({fn, key}) => {
const item = await fn();
if (typeof key !== "string") return null;
return <>{item[key]}</>
}









// Client component
const Component: ComponentType = ({fn, key}) => {
const [item, setItem] = useState();
useEffect(() => {
fn().then(setItem)
}, [])
if (typeof key !== "string") return null;
return <>{item[key]}</>
}






Another way to do it (with a function returning an array and using a helper):




type KeyOf<T> = T extends Record<infer K extends string, unknown> ? K : never;
async function Component<F extends () => Promise<Record<string, unknown>[]>>(fn: F, key: KeyOf<Awaited<ReturnType<F>>[number]>): React.JSX.Element {
const item = await fn()
// there is no need to check for key to be string
return <>{item[key]}</>;
}


Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Advanced typings

Thematisch verwandte Begriffe: Advanced, typings · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

💬 Kommentare werden geladen…
Zum Aktualisieren ziehen
ZERO-DAY CVE-2026-101132 | A security flaw has been discovered in DeepSeek deepseek-harness up to …
Advisory →
tsecurity.de Icon
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag