| name | typescript |
| description | TypeScript and unicorn linting patterns. Use for typescript, array, for-of, reduce, forEach, throw, catch, modern js, es modules, string, number, error handling |
TypeScript & Unicorn Patterns
This project uses unicorn.configs.recommended which enforces 100+ rules for modern JavaScript/TypeScript.
Arrays (IMPORTANT - Common Errors)
array.reduce((acc, item) => acc + item, 0);
let sum = 0;
for (const item of array) sum += item;
array.forEach((item) => console.log(item));
for (const item of array) console.log(item);
for (let i = 0; i < array.length; i++) {}
for (const item of array) {
}
for (const [index, item] of array.entries()) {
}
new Array(10);
Array.from({ length: 10 });
array.at(-1);
array.includes(x);
array.find((x) => x.id === id);
array.some((x) => x.valid);
array.flat();
array.flatMap((x) => x.items);
array.toSorted();
array.toReversed();
Strings
str.replaceAll('a', 'b');
str.slice(1, 3);
str.startsWith('x');
str.endsWith('x');
str.trimStart();
str.trimEnd();
str.codePointAt(0);
str.at(-1);
Errors
throw new Error('Something went wrong');
throw Error('msg');
throw 'error';
throw new Error();
catch (error) {}
catch (e) {}
catch (err) {}
if (typeof x !== 'string') throw new TypeError('Expected string');
Modern JavaScript
import x from 'module';
export { x };
const x = require('module');
module.exports = x;
import fs from 'node:fs';
import fs from 'fs';
globalThis.setTimeout;
window.setTimeout;
global.setTimeout;
[...array];
Array.from(array);
array.at(-1);
array[array.length - 1];
const data = await fetchData();
(async () => {
const data = await fetchData();
})();
Ternary & Conditionals
const x = a ? (b ? 1 : 2) : 3;
const x = condition ? 'a' : 'b';
const x = a ?? b;
const x = a || b;
if (!condition) {
} else {
}
if (condition) {
} else {
}
Functions & Classes
class Utils {
static helper() {}
}
const utils = { helper() {} };
function helper() {}
class Foo {
bar = 'value';
}
DOM (Browser Code)
element.append(child);
element.remove();
element.querySelector('.x');
element.closest('.x');
element.dataset.value;
element.addEventListener('click', fn);
element.classList.toggle('active');
event.key === 'Enter';
event.keyCode === 13;
Number & Math
Number.isNaN(x);
Number.isFinite(x);
Number.parseInt(x);
Math.trunc(x);
x | 0;
~~x;
const billion = 1_000_000_000;
Miscellaneous
if (array.length > 0) {
}
if (array.length) {
}
const set = new Set(array);
set.has(x);
Object.fromEntries(entries);
try {
} catch {}
export { x } from './module';
Common Mistakes
| Mistake | Correct Pattern |
|---|
array.reduce() | Use for-of loop |
array.forEach() | Use for-of loop |
for (let i = 0; ...) | Use for-of or array.entries() |
catch (e) | Use catch (error) |
throw 'error' | Use throw new Error('message') |
array[array.length - 1] | Use array.at(-1) |
[...array].sort() | Use array.toSorted() |
import fs from 'fs' | Use import fs from 'node:fs' |
Delegation
- Pattern discovery: Use
Explore agent to find existing patterns
- Linting issues: Run
pnpm lint:fix to auto-fix