Skip to content

Commit 9fce27d

Browse files
Fix one or the other prop pattern example
1 parent 608b33f commit 9fce27d

File tree

1 file changed

+19
-19
lines changed

1 file changed

+19
-19
lines changed

docs/advanced/patterns_by_usecase.md

+19-19
Original file line numberDiff line numberDiff line change
@@ -681,33 +681,33 @@ function isString(a: unknown): a is string {
681681

682682
See this quick guide: https://twitter.com/mpocock1/status/1500813765973053440?s=20&t=ImUA-NnZc4iUuPDx-XiMTA
683683

684-
## Props: One or the Other but not Both
684+
## Props: One or the other but not both
685685

686-
Use the `in` keyword, function overloading, and union types to make components that take either one or another sets of props, but not both:
686+
Use the `never` type to type components to take either one or another set of props, but not both:
687687

688688
```tsx
689-
type Props1 = { foo: string };
690-
type Props2 = { bar: string };
689+
type Props1 = { foo: string; bar?: never };
690+
type Props2 = { bar: string; foo?: never };
691691

692-
function MyComponent(props: Props1 | Props2) {
693-
if ("foo" in props) {
694-
// props.bar // error
695-
return <div>{props.foo}</div>;
696-
} else {
697-
// props.foo // error
698-
return <div>{props.bar}</div>;
692+
const OneOrTheOther = (props: Props1 | Props2) => {
693+
if (typeof props.foo === "string") {
694+
// `props.bar` is of type `never | undefined`
695+
return <>{props.foo}</>;
699696
}
700-
}
701-
const UsageComponent = () => (
702-
<div>
703-
<MyComponent foo="foo" />
704-
<MyComponent bar="bar" />
705-
{/* <MyComponent foo="foo" bar="bar"/> // invalid */}
706-
</div>
697+
// `props.foo` is of type `never | undefined`
698+
return <>{props.bar}</>;
699+
};
700+
const Component = () => (
701+
<>
702+
<OneOrTheOther /> {/* error */}
703+
<OneOrTheOther foo="" />
704+
<OneOrTheOther bar="" />
705+
<OneOrTheOther foo="" bar="" /> {/* error */}
706+
</>
707707
);
708708
```
709709

710-
[View in the TypeScript Playground](https://www.typescriptlang.org/play/?jsx=2#code/JYWwDg9gTgLgBAJQKYEMDG8BmUIjgcilQ3wFgAoCmATzCTgAUcwBnARjgF44BvOTCBABccFjCjAAdgHM4AXwDcVWvSYRWAJi684AIxRQRYiTPlLK5TAFdJGYBElwAstQDCuSJKSSYACjDMLCJqrBwAPoyBGgCUvBRwcMCYcL4ARAIQqYmOAeossTzxCXAA9CVwuawAdPpQpeVIUDhQRQlEMFZQjgA8ACbAAG4AfDyVLFUZct0l-cPmCXJwSAA2LPSF5MX1FYETgtuNza1w7Z09syNjNQZTM4ND8-IUchRoDmJwAKosKNJI7uAHN4YCJkOgYFUAGKubS+WKcIYpIp9e7HbouAGeYH8QScdKCLIlIZojEeIE+PQGPG1QnEzbFHglABUcHRbjJXgpGTxGSytWpBlSRO2UgGKGWwF6cCZJRe9OmFwo0QUQA)
710+
[View in the TypeScript Playground.](https://www.typescriptlang.org/play?jsx=4#code/JYWwDg9gTgLgBAJQKYEMDG8BmUIjgIilQ3wFgAoCmATzCTgAUcwBnARjgF44BvOTCBABccFjCjAAdgHMA3HABGKKAH4RkpADckUOAF8qtekwisATF16LlIsRJnyBENXA3bdByuTQRJYuADyGgFQACoAFkgBMJG63AAUYMwsIiasHAA+jMlmAJRcAHy8FHBwwJhw8TR0EBVJpiwAdE5cnNz4dlLS+Pk8JaVwAPSDcAAG9ayNSlCjZSxwtXDV9G46-aVEMACuUJJwADwFPBNNTnr7gwX9enBIADYs9H3kA0Mj48nNgrPA84vLri0axeA02Oz2h2On2m50u1wonh8fngAGFcJANJJ4Al8pwivF+od1gcglEwpForEhkUeIMAFS3KA4XR0waeV77UkhCJRGI6fiCTj4fDU4mc4Lk3lU6ZCkVwkGlcVknmU-lOWXWKAay68emM5lwVnsg7y3IUIA)
711711

712712
Further reading: [how to ban passing `{}` if you have a `NoFields` type.](http://www.javiercasas.com/articles/typescript-impossible-states-irrepresentable)
713713

0 commit comments

Comments
 (0)