* refactor(ui): replace runtime type guards with lodash-es predicates * refactor(runtime): migrate typeof checks to lodash-es in core modules * refactor(ipc): standardize runtime guards with lodash-es * test(refactor): align runtime guard checks with lodash-es * docs(agents): add lodash-es runtime guard skill summary
1.8 KiB
1.8 KiB
lodash-es-runtime-guards
Replace runtime typeof checks and ad-hoc empty-string checks with lodash-es predicates.
When to Use
- You see runtime checks like
typeof value === 'string'. - You see object guards like
value && typeof value === 'object'. - You see emptiness checks like
value.trim() !== ''orvalue.trim() === ''. - You want consistent guard style across app, server, IPC, and tests.
Do Not Replace
- Type-level
typeof(TypeScript only), for example:ReturnType<typeof fn>keyof typeof CONST_MAPz.infer<typeof Schema>
These are compile-time types and must remain as-is.
Guard Mapping
typeof x === 'string'->isString(x)typeof x !== 'string'->!isString(x)typeof x === 'number'->isNumber(x)typeof x === 'boolean'->isBoolean(x)typeof x === 'function'->isFunction(x)typeof x === 'undefined'->isUndefined(x)x && typeof x === 'object'->isObjectLike(x)typeof x === 'object' && x !== null->isObjectLike(x)typeof x === 'object' && !Array.isArray(x)->isObjectLike(x) && !isArray(x)
String Emptiness Pattern
- Prefer:
isEmpty(value.trim())
- Instead of:
value.trim() === ''value.trim() !== ''value.trim().length === 0
Import Style
- Use named imports from
lodash-es:
import { isString, isNumber, isObjectLike, isEmpty } from 'lodash-es';
- Do not use full-package imports.
- Reuse existing imports in file; avoid duplicate imports.
Refactor Checklist
- Replace runtime checks first, preserving behavior.
- Keep array/object branching semantics unchanged.
- Re-scan:
rg -n "typeof\s+[^\n]+(?:===|!==)\s*'" src
- Confirm only type-level
typeofremains:
rg -n "\btypeof\b" src
- Run verification:
npm run type-check