Skip to content

Commit 5bd3043

Browse files
authored
Rework gen-readme (typescript-cheatsheets#511)
1 parent b47c4c1 commit 5bd3043

File tree

7 files changed

+408
-585
lines changed

7 files changed

+408
-585
lines changed

Diff for: .github/workflows/main.yml

+16-19
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,34 @@
1-
name: FormatCodeAndShowDiff
1+
name: CI
22
on:
33
push:
44
branches: [main]
55
pull_request:
66
branches: [main]
77

88
jobs:
9-
prettier:
9+
files-up-to-date:
1010
runs-on: ubuntu-latest
1111
steps:
1212
- name: Checkout commit
13-
uses: actions/checkout@v2
14-
if: ${{ github.event_name == 'push' }}
15-
16-
- name: Checkout Pull Request
17-
uses: actions/checkout@v2
18-
with:
19-
# TODO: Can we unify the checkout steps i.e. does this value resolve to "undefined" on `push`?
20-
ref: ${{ github.event.pull_request.head.sha }}
21-
if: ${{ github.event_name == 'pull_request' }}
13+
uses: actions/checkout@v3
2214

23-
- name: Get yarn cache directory
24-
id: yarn
25-
run: echo "::set-output name=dir::$(yarn cache dir)"
15+
- name: Read .nvmrc
16+
run: echo ::set-output name=NVMRC::$(cat .nvmrc)
17+
id: nvm
2618

27-
- name: Restore yarn cache
28-
uses: actions/[email protected]
19+
- name: Setup node
20+
uses: actions/setup-node@v3
2921
with:
30-
path: ${{ steps.yarn.outputs.dir }}
31-
key: ${{ runner.os }}-yarn-${{ hashFiles(format('{0}{1}', github.workspace, '/website/yarn.lock')) }}
22+
node-version: "${{ steps.nvm.outputs.NVMRC }}"
23+
cache: yarn
3224

3325
- name: Install dependencies
3426
run: yarn install --frozen-lockfile
3527

36-
- name: '`yarn format` changes committed?'
28+
- name: "`yarn format` changes committed?"
3729
run: yarn format:check
30+
31+
- name: "`yarn gen-readme` changes committed?"
32+
run: |
33+
yarn gen-readme
34+
git diff --exit-code

Diff for: .github/workflows/readme.js.yml

-26
This file was deleted.

Diff for: .nvmrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
v16.15.1

Diff for: README.md

+13-14
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ function Foo() {
577577

578578
If you are sure that `divRef.current` will never be null, it is also possible to use the non-null assertion operator `!`:
579579

580-
```
580+
```tsx
581581
const divRef = useRef<HTMLDivElement>(null!);
582582
// Later... No need to check if it is null
583583
doSomethingWith(divRef.current);
@@ -1163,12 +1163,8 @@ Relevant for components that accept other React components as props.
11631163

11641164
```tsx
11651165
export declare interface AppProps {
1166-
children1: JSX.Element; // bad, doesnt account for arrays
1167-
children2: JSX.Element | JSX.Element[]; // meh, doesn't accept strings
1168-
children3: React.ReactChildren; // despite the name, not at all an appropriate type; it is a utility
1169-
children4: React.ReactChild[]; // better, accepts array children
1170-
children: React.ReactNode; // best, accepts everything (see edge case below)
1171-
functionChildren: (name: string) => React.ReactNode; // recommended function as a child render prop type
1166+
children?: React.ReactNode; // best, accepts everything React can render
1167+
childrenElement: JSX.Element; // A single React element
11721168
style?: React.CSSProperties; // to pass through style props
11731169
onChange?: React.FormEventHandler<HTMLInputElement>; // form events! the generic parameter is the type of event.target
11741170
// more info: https://react-typescript-cheatsheet.netlify.app/docs/advanced/patterns_by_usecase/#wrappingmirroring
@@ -1184,7 +1180,7 @@ Before the [React 18 type updates](https://github.com/DefinitelyTyped/Definitely
11841180

11851181
```tsx
11861182
type Props = {
1187-
children: React.ReactNode;
1183+
children?: React.ReactNode;
11881184
};
11891185

11901186
function Comp({ children }: Props) {
@@ -1726,7 +1722,10 @@ interface ProviderStore {
17261722

17271723
const Context = React.createContext({} as ProviderStore); // type assertion on empty object
17281724

1729-
class Provider extends React.Component<{}, ProviderState> {
1725+
class Provider extends React.Component<
1726+
{ children?: React.ReactNode },
1727+
ProviderState
1728+
> {
17301729
public readonly state = {
17311730
themeColor: "red",
17321731
};
@@ -1776,7 +1775,7 @@ class CssThemeProvider extends React.PureComponent<Props> {
17761775
`forwardRef`:
17771776

17781777
```tsx
1779-
type Props = { children: React.ReactNode; type: "submit" | "button" };
1778+
type Props = { children?: React.ReactNode; type: "submit" | "button" };
17801779
export type Ref = HTMLButtonElement;
17811780
export const FancyButton = React.forwardRef<Ref, Props>((props, ref) => (
17821781
<button ref={ref} className="MyClassName" type={props.type}>
@@ -1791,7 +1790,7 @@ export const FancyButton = React.forwardRef<Ref, Props>((props, ref) => (
17911790
This was done [on purpose](https://github.com/DefinitelyTyped/DefinitelyTyped/pull/43265/). You can make it immutable if you have to - assign `React.Ref` if you want to ensure nobody reassigns it:
17921791

17931792
```tsx
1794-
type Props = { children: React.ReactNode; type: "submit" | "button" };
1793+
type Props = { children?: React.ReactNode; type: "submit" | "button" };
17951794
export type Ref = HTMLButtonElement;
17961795
export const FancyButton = React.forwardRef(
17971796
(
@@ -1891,7 +1890,7 @@ Using `ReactDOM.createPortal`:
18911890
const modalRoot = document.getElementById("modal-root") as HTMLElement;
18921891
// assuming in your html file has a div with id 'modal-root';
18931892

1894-
export class Modal extends React.Component {
1893+
export class Modal extends React.Component<{ children?: React.ReactNode }> {
18951894
el: HTMLElement = document.createElement("div");
18961895

18971896
componentDidMount() {
@@ -1921,7 +1920,7 @@ import { createPortal } from "react-dom";
19211920

19221921
const modalRoot = document.querySelector("#modal-root") as HTMLElement;
19231922

1924-
const Modal: React.FC<{}> = ({ children }) => {
1923+
const Modal: React.FC<{ children?: React.ReactNode }> = ({ children }) => {
19251924
const el = useRef(document.createElement("div"));
19261925

19271926
useEffect(() => {
@@ -2008,7 +2007,7 @@ If you don't want to add a new npm package for this, you can also write your own
20082007
import React, { Component, ErrorInfo, ReactNode } from "react";
20092008

20102009
interface Props {
2011-
children: ReactNode;
2010+
children?: ReactNode;
20122011
}
20132012

20142013
interface State {

0 commit comments

Comments
 (0)