From ca5b7cc2df056e70d6248fe4fef994b22cfd9cd2 Mon Sep 17 00:00:00 2001 From: Maxime Quandalle Date: Wed, 18 Dec 2019 17:47:43 +0100 Subject: [PATCH] Ajout des types null et undefined MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Ajout des paramètres strictNullChecks et strictPropertyInitialization dans la configuration TypeScript et correction des environ 70 erreurs de typage résultantes. --- source/Provider.tsx | 7 +-- source/Tracker.ts | 4 +- source/actions/actions.ts | 22 +++++---- source/api/sirene.ts | 4 +- .../CurrencyInput/CurrencyInput.tsx | 10 ++--- source/components/Feedback/FeedbackForm.tsx | 4 +- source/components/FindCompany.tsx | 4 +- source/components/QuickLinks.tsx | 38 ++++++++-------- source/components/SalaryExplanation.tsx | 6 +-- source/components/SearchBar.tsx | 10 +++-- source/components/StackedBarChart.tsx | 2 +- source/components/TargetSelection.tsx | 2 +- source/components/Value.tsx | 6 +-- .../conversation/InputSuggestions.tsx | 4 +- source/components/ui/AnimatedTargetValue.tsx | 8 ++-- source/components/utils/persistState.ts | 2 +- .../utils/useDisplayOnIntersecting.ts | 8 ++-- source/components/utils/withColours.tsx | 2 +- source/engine/format.ts | 10 ++--- source/engine/generateQuestions.ts | 8 +++- source/engine/mecanisms/encadrement.tsx | 9 ++-- source/engine/traverse.ts | 12 ++--- source/engine/units.ts | 4 +- source/reducers/inFranceAppReducer.ts | 9 ++-- source/reducers/rootReducer.ts | 38 ++++++++-------- source/selectors/analyseSelectors.ts | 10 ++--- source/selectors/companyStatusSelectors.ts | 7 ++- source/selectors/ficheDePaieSelectors.ts | 2 +- source/selectors/storageSelectors.ts | 34 +++++++------- .../pages/Créer/AfterRegistration.tsx | 2 +- .../Créer/GuideStatut/PreviousAnswers.tsx | 29 ++++++------ .../pages/Dev/IntegrationTest.tsx | 6 ++- .../pages/Gérer/AideOrganismeLocal.tsx | 1 + .../mon-entreprise.fr/pages/Gérer/Home.tsx | 10 ++--- .../pages/Simulateurs/ArtisteAuteur.tsx | 45 ++++++++++--------- source/utils.ts | 14 +++--- tsconfig.json | 4 ++ 37 files changed, 216 insertions(+), 181 deletions(-) diff --git a/source/Provider.tsx b/source/Provider.tsx index 613dce56a..919167db2 100644 --- a/source/Provider.tsx +++ b/source/Provider.tsx @@ -95,9 +95,10 @@ export default class Provider extends PureComponent { this.props.tracker.disconnectFromHistory() } render() { - const iframeCouleur = new URLSearchParams( - document?.location.search.substring(1) - ).get('couleur') + const iframeCouleur = + new URLSearchParams(document?.location.search.substring(1)).get( + 'couleur' + ) ?? undefined return ( // If IE < 11 display nothing diff --git a/source/Tracker.ts b/source/Tracker.ts index dcd80230f..830ee274b 100644 --- a/source/Tracker.ts +++ b/source/Tracker.ts @@ -12,8 +12,8 @@ type PushType = (args: PushArgs) => void export default class Tracker { push: PushType - unlistenFromHistory: () => void - previousPath: string + unlistenFromHistory: (() => void) | undefined + previousPath: string | undefined constructor(pushFunction: PushType = args => window._paq.push(args)) { if (typeof window !== 'undefined') window._paq = window._paq || [] diff --git a/source/actions/actions.ts b/source/actions/actions.ts index 33c81d739..72800aa11 100644 --- a/source/actions/actions.ts +++ b/source/actions/actions.ts @@ -43,12 +43,17 @@ type DeletePreviousSimulationAction = { type: 'DELETE_PREVIOUS_SIMULATION' } -type SetExempleAction = { - type: 'SET_EXAMPLE' - name: null | string - situation?: object - dottedName?: string -} +type SetExempleAction = + | { + type: 'SET_EXAMPLE' + name: null + } + | { + type: 'SET_EXAMPLE' + name: string + situation: object + dottedName: DottedName + } type ResetSimulationAction = ReturnType type UpdateAction = ReturnType @@ -142,7 +147,8 @@ export const goBackToSimulation = (): ThunkResult => ( { history } ) => { dispatch({ type: 'SET_EXAMPLE', name: null }) - history.push(getState().simulation.url) + const url = getState().simulation?.url + url && history.push(url) } export function loadPreviousSimulation() { @@ -155,7 +161,7 @@ export function hideControl(id: string) { return { type: 'HIDE_CONTROL', id } as const } -export const explainVariable = (variableName = null) => +export const explainVariable = (variableName: DottedName | null = null) => ({ type: 'EXPLAIN_VARIABLE', variableName diff --git a/source/api/sirene.ts b/source/api/sirene.ts index 8df00bf7a..fb9fdd28a 100644 --- a/source/api/sirene.ts +++ b/source/api/sirene.ts @@ -30,7 +30,9 @@ export type Etablissement = { denomination?: string } -async function searchFullText(text: string): Promise> { +async function searchFullText( + text: string +): Promise | null> { const response = await fetch( `https://entreprise.data.gouv.fr/api/sirene/v1/full_text/${text}?per_page=5` ) diff --git a/source/components/CurrencyInput/CurrencyInput.tsx b/source/components/CurrencyInput/CurrencyInput.tsx index f8d892146..eb5dd5710 100644 --- a/source/components/CurrencyInput/CurrencyInput.tsx +++ b/source/components/CurrencyInput/CurrencyInput.tsx @@ -25,11 +25,11 @@ export default function CurrencyInput({ const [initialValue, setInitialValue] = useState(valueProp) const [currentValue, setCurrentValue] = useState(valueProp) const onChangeDebounced = useRef( - debounceTimeout ? debounce(debounceTimeout, onChange) : onChange + debounceTimeout && onChange ? debounce(debounceTimeout, onChange) : onChange ) // We need some mutable reference because the component doesn't provide // the DOM `event` in its custom `onValueChange` handler - const nextValue = useRef(null) + const nextValue = useRef('') const inputRef = useRef() @@ -51,8 +51,8 @@ export default function CurrencyInput({ ...event.target, value: nextValue.current } - nextValue.current = null - onChangeDebounced.current(event) + nextValue.current = '' + onChangeDebounced.current?.(event) } const { @@ -71,7 +71,7 @@ export default function CurrencyInput({
5 ? { style: { width } } : {})} - onClick={() => inputRef.current.focus()} + onClick={() => inputRef.current?.focus()} > {!currentValue && isCurrencyPrefixed && currencySymbol} void; onCancel: () => void } export default function FeedbackForm({ onEnd, onCancel }: Props) { - const formRef = useRef() + const formRef = useRef(null) const tracker = useContext(TrackerContext) const handleFormSubmit = (e: React.FormEvent): void => { @@ -14,7 +14,7 @@ export default function FeedbackForm({ onEnd, onCancel }: Props) { e.preventDefault() fetch('/', { method: 'POST', - body: new FormData(formRef.current) + body: new FormData(formRef.current ?? undefined) }) onEnd() } diff --git a/source/components/FindCompany.tsx b/source/components/FindCompany.tsx index 351b073ce..71c007abe 100644 --- a/source/components/FindCompany.tsx +++ b/source/components/FindCompany.tsx @@ -7,7 +7,9 @@ import { Etablissement, searchDenominationOrSiren } from '../api/sirene' import { debounce } from '../utils' export default function Search() { - const [searchResults, setSearchResults] = useState>() + const [searchResults, setSearchResults] = useState | null>() const [isLoading, setLoadingState] = useState(false) const handleSearch = useCallback( diff --git a/source/components/QuickLinks.tsx b/source/components/QuickLinks.tsx index bbcee1971..d4886f2e6 100644 --- a/source/components/QuickLinks.tsx +++ b/source/components/QuickLinks.tsx @@ -30,24 +30,26 @@ export default function QuickLinks() { toPairs )(quickLinks) + if (links.length < 1) { + return null + } + return ( - !!links.length && ( - - Questions : - {links.map(([label, dottedName]) => ( - - ))}{' '} - {/* */} - - ) + + Questions : + {links.map(([label, dottedName]) => ( + + ))}{' '} + {/* */} + ) } diff --git a/source/components/SalaryExplanation.tsx b/source/components/SalaryExplanation.tsx index e267652a0..add7f7459 100644 --- a/source/components/SalaryExplanation.tsx +++ b/source/components/SalaryExplanation.tsx @@ -34,7 +34,7 @@ export default function SalaryExplanation() { const showDistributionFirst = useSelector( (state: RootState) => !state.conversationSteps.foldedSteps.length ) - const distributionRef = useRef() + const distributionRef = useRef(null) return ( @@ -51,7 +51,7 @@ export default function SalaryExplanation() {