-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
Type assertions can lead to runtime errors and should be avoided. Two alternatives: 1. Instead of asserting that certain resource properties are available through type assertions, check that they are actually available with null checks that will return null instead of an unexpected error. 2. Instead of asserting that array elements accessed via index are available through type assertions, use a non-null assertion which effectively does the same thing but without the need to cast the original type. This leaves just one type assertion (parentNode as HTMLElement) with added commentary on why it's necessary.
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -4,16 +4,16 @@ import styled from 'styled-components'; | |||||||||
import * as d3 from "d3"; | ||||||||||
import { MdClose } from "react-icons/md"; | ||||||||||
import { dodge } from "./dodge"; | ||||||||||
import { VersionedResource } from './types'; | ||||||||||
import { Resource } from './types'; | ||||||||||
|
||||||||||
export const SetModalResourceContext = createContext<React.Dispatch<React.SetStateAction<VersionedResource | undefined>> | null>(null); | ||||||||||
export const SetModalResourceContext = createContext<React.Dispatch<React.SetStateAction<Resource | undefined>> | null>(null); | ||||||||||
|
||||||||||
export const RAINBOW20 = ["#511EA8", "#4432BD", "#3F4BCA", "#4065CF", "#447ECC", "#4C91BF", "#56A0AE", "#63AC9A", "#71B486", "#81BA72", "#94BD62", "#A7BE54", "#BABC4A", "#CBB742", "#D9AE3E", "#E29E39", "#E68935", "#E56E30", "#E14F2A", "#DC2F24"]; | ||||||||||
const lightGrey = 'rgba(0,0,0,0.1)'; | ||||||||||
|
||||||||||
|
||||||||||
interface ResourceModalProps { | ||||||||||
resource?: VersionedResource | ||||||||||
resource?: Resource | ||||||||||
dismissModal: () => void | ||||||||||
} | ||||||||||
|
||||||||||
|
@@ -41,7 +41,7 @@ export const ResourceModal = ({resource, dismissModal}: ResourceModalProps) => { | |||||||||
_draw(ref, resource) | ||||||||||
}, [ref, resource]) | ||||||||||
|
||||||||||
if (!resource) return null; | ||||||||||
if (!resource || !resource.dates || !resource.updateCadence) return null; | ||||||||||
|
||||||||||
const summary = _snapshotSummary(resource.dates); | ||||||||||
return ( | ||||||||||
|
@@ -132,8 +132,8 @@ const Title = styled.div` | |||||||||
|
||||||||||
function _snapshotSummary(dates: string[]) { | ||||||||||
const d = [...dates].sort() | ||||||||||
const d1 = new Date(d.at( 0) as string).getTime(); | ||||||||||
const d2 = new Date(d.at(-1) as string).getTime(); | ||||||||||
const d1 = new Date(d.at( 0)!).getTime(); | ||||||||||
const d2 = new Date(d.at(-1)!).getTime(); | ||||||||||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
victorlin
Author
Member
|
if (d.length < 1) throw new Error("Missing dates.") | |
const d1 = new Date(d.at( 0)!).getTime(); | |
const d2 = new Date(d.at(-1)!).getTime(); |
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -102,7 +102,7 @@ const CardOuter = styled.div` | |||||||||||||||
|
||||||||||||||||
const themeColors = [...theme.titleColors]; | ||||||||||||||||
const getColor = () => { | ||||||||||||||||
themeColors.push(themeColors.shift() as string); // this will always be string; type assertion is necessary to appease tsc | ||||||||||||||||
themeColors.push(themeColors.shift()!); // this will always be string; non-null assertion is necessary to appease tsc | ||||||||||||||||
This comment has been minimized.
Sorry, something went wrong.
ivan-aksamentov
Member
|
const themeColors = [...theme.titleColors]; | |
const getColor = () => { | |
// rotate colors by moving the first color (which is always defined) to the end | |
themeColors.push(themeColors.shift()!); | |
// return the last color | |
return themeColors.at(-1); | |
} |
If `dates` always contains 2 elements, then tuple type will be better here:
Most importantly it avoids deceiving the compiler and prevents runtime errors from happening.
Totally misread this code.
I'd use a wrapper something like
first()
andlast()
, orfirstAndLast()
which throw on incorrect input.