Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TypeScript Rollout Tier 9 - Datepicker (#379)
* feat(lib): rewrite datepicker in TS Rewrites the datepicker components in the `src/components/datepicker` folder in TypeScript. Adds a new file `types.ts` which defines common types shared among the datepicker components. Makes some props explicitly allow `null` to suppress type errors at lines where `null` is assigned to them. Applies the `valueOf` method to `Date` when it is passed to `isNaN` because `isNaN` allows only a number. It should not matter to the decision. In some cases where the `Date` parameter may be `undefined`, we have to be careful not to apply `valueOf` to `undefined` and replace `undefined` with `NaN`. Replaces some `&&` chaining with ternary conditional operators (`?:`), where a fallback value should be `undefined` rather than a boolean (`false`). Overuses non-null assertion operators (`!`) to suppress type errors while keeping the original behavior. I decided not to use optional chaining operators (`?`) unless unavoidable, because they change the pass of the execution. Overuses type casting (`as`) to suppress type errors while keeping the original behavior. I did not take approaches that were more sophisticated but involving logic changes, since we decided to change the code, especially its logics, as little as possible during the migration process. Some trivial changes: - Wraps the entire component definition in a `defineComponent` call - All the Buefy components are directly named when they are registered; the `name` fields are no longer used. Otherwise type-checking won't work. - Replaces JSDoc-style comments with ordinary comments so that `@microsoft/api-extractor` won't pick them up for documentation. * test(lib): rewrite spec for datepicker in TS Rewrites the specs for the datepicker components in the `src/components/datepicker` in TypeScript. In `Datepicker.spec.js→ts`: - Hoists the `defaultProps` fixture to the top level so that its type is implicitly defined. And makes it a function so that it reflects the configuration made by `setOptions`. - `focusedDate` is no longer injected into `config` via `setOptions` but given as an independent constant. Because `focusedDate` is not a member of `BuefyConfig`, and `BuefyConfig` is not a container of arbitrary values. In `DatepickerMonth.spec.js→ts`: - `newDate` checks if the `y`, `m`, and `d` arguments are defined, because `Date` does not like them undefined. Creates `Date` without arguments if they are omitted. - `focusedDate` is no longer injected into `config` via `setOptions` but given as an independent constant. See the comments for `Datepicker.spec` for why. - Gives a `Date` instead of a number to the following methods: - `emitChosenDate` - `updateSelectedDate` Allowing a number to these methods does not make sense. In `DatepickerTable.spec.js→ts`: - Hoists the `defaultProps` fixture to the top level so that its type can be implicitly defined - `focusedDate` is no longer injected into `config` via `setOptions` but given as an independentconstant. See the comments for `Datepicker.spec` for why. In `DatepickerTableRow.spec.js→ts`: - `newDate` checks if the `y`, `m`, and `d` arguments are defined, because `Date` does not like them undefined. Create `Date` without arguments if they are omitted. - Gives a `Date` instead of a number to the following methods: - `emitChosenDate` - `updateSelectedDate` Allowing a number to these methods does not make sense. - `$emit` of the injected fake `$datepicker` explicitly receives parameters to suppress the type errors regarding `arguments`. Common in the spec files: - `newDate` checks if the `y`, `m`, and `d` arguments are defined. In the `__snapshots__` subfolder, replaces the spec snapshots produced by Jest with those by Vitest: - `Datepicker.spec.js.snap` → `Datepicker.spec.ts.snap` - `DatepickerMonth.spec.js.snap` → `DatepickerMonth.spec.ts.snap` - `DatepickerTable.spec.js.snap` → `DatepickerTable.spec.ts.snap` - `DatepickerTableRow.spec.js.snap` → `DatepickerTableRow.spec.ts.snap` Migrates from Jest to Vitest: - Imports the building blocks for the spec from the `vitest` package - Replaces `jest` with `vi` * chore(lib): bundle datepicker in TS `rollup.config.mjs` removes "datepicker" from `JS_COMPONENTS`. * feat(docs): rewrite docs for datepicker in TS Rewrites the documentation for the datepicker components in the `src/pages/components/datepicker` folder in TypeScript. All the changes are straightforward. Here is a TypeScript migration tip: - Explicitly import and register components so that they are type checked. No type-checking is performed for globally registered components.
- Loading branch information