Skip to content
This repository has been archived by the owner on Nov 11, 2023. It is now read-only.

Commit

Permalink
Add bang operator for default props
Browse files Browse the repository at this point in the history
  • Loading branch information
Tejas Kumar committed Aug 8, 2018
1 parent 42694a2 commit 89921b0
Showing 1 changed file with 3 additions and 9 deletions.
12 changes: 3 additions & 9 deletions src/Get.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,6 @@ export interface GetComponentProps<T = {}> {
base?: string;
}

interface GetComponentDefaultProps<T> {
resolve: ResolveFunction<T>;
}

/**
* State for the <Get /> component. These
* are implementation details and should be
Expand All @@ -96,8 +92,6 @@ export interface GetComponentState<T> {
loading: boolean;
}

type PropsWithDefaults<T> = GetComponentProps<T> & GetComponentDefaultProps<T>;

/**
* The <Get /> component without Context. This
* is a named class because it is useful in
Expand All @@ -111,7 +105,7 @@ class ContextlessGet<T> extends React.Component<GetComponentProps<T>, Readonly<G
loading: !this.props.lazy,
};

public static defaultProps: GetComponentDefaultProps<{}> = {
public static defaultProps: Partial<GetComponentProps<{}>> = {
resolve: (unresolvedData: any) => unresolvedData,
};

Expand Down Expand Up @@ -161,7 +155,7 @@ class ContextlessGet<T> extends React.Component<GetComponentProps<T>, Readonly<G
};

public fetch = async (requestPath?: string, thisRequestOptions?: RequestInit) => {
const { base, path, resolve } = this.props as PropsWithDefaults<T>;
const { base, path, resolve } = this.props;
this.setState(() => ({ error: "", loading: true }));

const request = new Request(`${base}${requestPath || path || ""}`, this.getRequestOptions(thisRequestOptions));
Expand All @@ -175,7 +169,7 @@ class ContextlessGet<T> extends React.Component<GetComponentProps<T>, Readonly<G
const data: T =
response.headers.get("content-type") === "application/json" ? await response.json() : await response.text();

this.setState({ loading: false, data: resolve(data) });
this.setState({ loading: false, data: resolve!(data) });
return data;
};

Expand Down

0 comments on commit 89921b0

Please sign in to comment.