Skip to content

Commit 665fa01

Browse files
fix(types): improve type safety in command helpers (#7852)
* feat: Improve type safety in command-helpers This commit improves the type safety of `src/utils/command-helpers.ts` by: - Adding a `boolean` type to the `noColors` parameter in `safeChalk`. - Adding `string` and `number` types to the parameters of `padLeft`. - Importing the `Option` type from `commander` to correctly type the `sortOptions` function parameters and adding defensive checks to handle potentially `undefined` properties at runtime. These changes eliminate three `@ts-expect-error` instances and strengthen the module's type safety and runtime robustness. * chore: Use type-only import for commander Option This commit updates the import of the `Option` type from `commander` to be a type-only import, following a suggestion from the code review. This is a best practice that makes the dependency explicit as a type-only dependency. --------- Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
1 parent f5b1c11 commit 665fa01

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

src/utils/command-helpers.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { format, inspect } from 'util'
66
import type { NetlifyAPI } from '@netlify/api'
77
import { getAPIToken } from '@netlify/dev-utils'
88
import { Chalk, type ChalkInstance as ChalkInstancePrimitiveType } from 'chalk'
9+
import type { Option } from 'commander'
910
import WSL from 'is-wsl'
1011
import terminalLink from 'terminal-link'
1112

@@ -24,8 +25,7 @@ const argv = process.argv.slice(2)
2425
* @param {boolean} noColors - disable chalk colors
2526
* @return {ChalkInstancePrimitiveType} - default or custom chalk instance
2627
*/
27-
// @ts-expect-error TS(7006) FIXME: Parameter 'noColors' implicitly has an 'any' type.
28-
const safeChalk = function (noColors) {
28+
const safeChalk = function (noColors: boolean) {
2929
if (noColors) {
3030
const colorlessChalk = new Chalk({ level: 0 })
3131
return colorlessChalk
@@ -44,8 +44,7 @@ export type ChalkInstance = ChalkInstancePrimitiveType
4444
* @param {string} [filler]
4545
* @returns {string}
4646
*/
47-
// @ts-expect-error TS(7006) FIXME: Parameter 'str' implicitly has an 'any' type.
48-
export const padLeft = (str, count, filler = ' ') => str.padStart(str.length + count, filler)
47+
export const padLeft = (str: string, count: number, filler = ' ') => str.padStart(str.length + count, filler)
4948

5049
const platform = WSL ? 'wsl' : os.platform()
5150
const arch = os.arch() === 'ia32' ? 'x86' : os.arch()
@@ -77,13 +76,12 @@ export const BANG = process.platform === 'win32' ? '»' : '›'
7776
* @example
7877
* options.sort(sortOptions)
7978
*/
80-
// @ts-expect-error TS(7006) FIXME: Parameter 'optionA' implicitly has an 'any' type.
81-
export const sortOptions = (optionA, optionB) => {
79+
export const sortOptions = (optionA: Option, optionB: Option) => {
8280
// base flags should be always at the bottom
83-
if (BASE_FLAGS.has(optionA.long) || BASE_FLAGS.has(optionB.long)) {
81+
if ((optionA.long && BASE_FLAGS.has(optionA.long)) || (optionB.long && BASE_FLAGS.has(optionB.long))) {
8482
return -1
8583
}
86-
return optionA.long.localeCompare(optionB.long)
84+
return (optionA.long ?? '').localeCompare(optionB.long ?? '')
8785
}
8886

8987
// Poll Token timeout 5 Minutes

0 commit comments

Comments
 (0)