Skip to content

Commit

Permalink
Add strict type-checks that most switch statements cover all cases
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaw3d committed Jun 8, 2023
1 parent b702bc7 commit 3b7e1c3
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 14 deletions.
4 changes: 3 additions & 1 deletion src/app/components/Transactions/LogEvent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { AccountLink } from '../Account/AccountLink'
import { CopyToClipboard } from '../CopyToClipboard'
import { TransactionLink } from './TransactionLink'
import { SearchScope } from '../../../types/searchScope'
import { ExhaustedTypeError } from '../../../types/errors'

const EvmEventParamData: FC<{ scope: SearchScope; param: EvmEventParam }> = ({ scope, param }) => {
/**
Expand Down Expand Up @@ -111,13 +112,14 @@ const DecodedLogEvent: FC<{ scope: SearchScope; event: RuntimeEvent }> = ({ scop
case RuntimeEventType.accountstransfer:
case RuntimeEventType.consensus_accountsdeposit:
case RuntimeEventType.consensus_accountswithdraw:
default:
return (
<div>
<div>{eventName}</div>
<pre>{JSON.stringify(event, null, ' ')}</pre>
</div>
)
default:
throw new ExhaustedTypeError('Unexpected event type', event.type)
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/app/pages/HomePage/Graph/Graph/graph-utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ScaleToOptions } from 'react-quick-pinch-zoom'
import { Layer } from '../../../../../oasis-indexer/api'
import { ExhaustedTypeError } from '../../../../../types/errors'

export abstract class GraphUtils {
static getScaleTo(layer: Layer, { width, height }: { width?: number; height?: number }): ScaleToOptions {
Expand Down Expand Up @@ -33,8 +34,9 @@ export abstract class GraphUtils {
y: height,
}
case Layer.consensus:
default:
return initialValue
default:
throw new ExhaustedTypeError('Unexpected layer', layer)
}
}
}
6 changes: 5 additions & 1 deletion src/app/pages/HomePage/Graph/para-time-selector-utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ParaTimeSelectorStep } from './types'
import { Layer } from '../../../../oasis-indexer/api'
import { ExhaustedTypeError } from '../../../../types/errors'

export abstract class ParaTimeSelectorUtils {
static getIsGraphTransparent(step: ParaTimeSelectorStep) {
Expand Down Expand Up @@ -28,8 +29,11 @@ export abstract class ParaTimeSelectorUtils {
case Layer.emerald:
case Layer.cipher:
return true
default:
case Layer.consensus:
case undefined:
return false
default:
throw new ExhaustedTypeError('Unexpected layer', layer)
}
}
}
7 changes: 2 additions & 5 deletions src/app/pages/SearchResultsPage/useRedirectIfSingleResult.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { SearchResults } from './hooks'
import { RouteUtils } from '../../utils/route-utils'
import { isItemInScope, SearchScope } from '../../../types/searchScope'
import { Network } from '../../../types/network'
import { ExhaustedTypeError } from '../../../types/errors'

/** If search only finds one result then redirect to it */
export function useRedirectIfSingleResult(
Expand Down Expand Up @@ -34,11 +35,7 @@ export function useRedirectIfSingleResult(
redirectTo = RouteUtils.getAccountRoute(item, item.address_eth ?? item.address)
break
default:
// The conversion of any is necessary here, since we have covered all possible subtype,
// and TS is concluding that the only possible remaining type is "never".
// However, if we all more result types in the future and forget to add the appropriate case here,
// we might hit this, hence the warning.
console.log(`Don't know how to redirect to unknown search result type ${(item as any).resultType}`)
throw new ExhaustedTypeError('Unexpected result type', item)
}
}

Expand Down
8 changes: 2 additions & 6 deletions src/coin-gecko/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useQuery } from '@tanstack/react-query'
import { getTickerForNetwork, NativeTicker, Ticker } from '../types/ticker'
import { Network } from '../types/network'
import { RouteUtils } from '../app/utils/route-utils'
import { ExhaustedTypeError } from '../types/errors'

type GetRosePriceParams = {
ids: string
Expand Down Expand Up @@ -68,12 +69,7 @@ export const useTokenPrice = (ticker: NativeTicker): TokenPriceInfo => {
isFree: true,
}
default:
console.warn('Checking price of unknown token', ticker)
return {
isLoading: false,
hasUsedCoinGecko: false,
isFree: false,
}
throw new ExhaustedTypeError('Checking price of unknown token', ticker)
}
}

Expand Down

0 comments on commit 3b7e1c3

Please sign in to comment.