Loading...
Loading...
This skill should be used when the user asks to "write TypeScript code", "format TypeScript", "follow TypeScript style guide", "TypeScript best practices", or needs guidance on Google's TypeScript coding conventions.
npx skill4agent add the-perfect-developer/the-perfect-opencode typescript-style\'\"\\\n\r\t// Good: clear unicode character
const units = 'μs';
// Good: escape with comment
const output = '\ufeff' + content; // byte order mark@fileoverview/**
* @fileoverview Description of file. Lorem ipsum dolor sit amet, consectetur
* adipiscing elit, sed do eiusmod tempor incididunt.
*/
import * as foo from './foo';
import {Bar} from './bar';
export class MyClass { }import * as foo from '...';import {SomeThing} from '...';import SomeThing from '...';import '...';// Good: module import for namespace
import * as ng from '@angular/core';
// Good: named import for clear symbols
import {describe, it, expect} from './testing';
// Only when needed
import Button from 'Button';// Good: named export
export class Foo { }
export const BAR = 42;// Bad: default export
export default class Foo { }./foo../../../import {Symbol1} from 'path/from/root';
import {Symbol2} from '../parent/file';
import {Symbol3} from './sibling';constletvarconst foo = otherValue; // Use const by default
let bar = someValue; // Use let when reassigningvar foo = someValue; // Never use varconstletvar// Good
let a = 1;
let b = 2;// Bad
let a = 1, b = 2;Array// Good
const a = [2];
const b = [2, 3];
const c = Array.from<number>({length: 5}).fill(0);// Bad: confusing behavior
const a = new Array(2); // [undefined, undefined]
const b = new Array(2, 3); // [2, 3]Object// Good
const obj = {};
const obj2 = {a: 0, b: 1};// Bad
const obj = new Object();// Good: array spread
const foo = [1, 2];
const foo2 = [...foo, 6, 7];
// Good: object spread
const foo = {num: 1};
const foo2 = {...foo, num: 5};// Good: array destructuring
const [a, b, c, ...rest] = generateResults();
let [, b, , d] = someArray;
// Good: object destructuring
const {num, str = 'default'} = options;// Good
function destructured({num, str = 'default'}: Options = {}) { }// Bad: too deeply nested
function nestedTooDeeply({x: {num, str}}: {x: Options}) { }// Good
class Foo {
}// Bad
class Foo {
};readonlyclass Foo {
private readonly bar = 5;
}// Good
class Foo {
constructor(private readonly barService: BarService) {}
}// Bad: unnecessary boilerplate
class Foo {
private readonly barService: BarService;
constructor(barService: BarService) {
this.barService = barService;
}
}// Good
class Foo {
private readonly userList: string[] = [];
}// Bad: unnecessary constructor
class Foo {
private readonly userList: string[];
constructor() {
this.userList = [];
}
}public// Good
class Foo {
bar = new Bar(); // public by default
constructor(public baz: Baz) {} // public modifier allowed
}// Bad
class Foo {
public bar = new Bar(); // unnecessary
}#identprivate// Good
class Clazz {
private ident = 1;
}// Bad
class Clazz {
#ident = 1;
}// Good
function foo() {
return 42;
}// Bad
const foo = () => 42;this// Good: callback
bar(() => { this.doSomething(); });
// Good: nested function with this
class Foo {
method() {
setTimeout(() => {
this.doWork();
}, 100);
}
}// Good: return value used
const numbers = [1, 2, 3].map(v => v * 2);
// Good: no return value
myPromise.then(v => {
console.log(v);
});
// Good: explicit void
myPromise.then(v => void console.log(v));this// Bad
function clickHandler() {
this.textContent = 'Hello';
}
document.body.onclick = clickHandler;// Good: use arrow function
document.body.onclick = () => {
document.body.textContent = 'hello';
};// Good: type is obvious
const x = 5;
const y = new Foo();
// Bad: redundant annotation
const x: number = 5;// Good
export function foo(): string {
return 'bar';
}interfacetypeinterface// Good: interface for object shape
interface User {
name: string;
age: number;
}
// Good: type for union
type Status = 'success' | 'error' | 'pending';UpperCamelCaselowerCamelCaseCONSTANT_CASEprivateUpperCamelCaseclass MyClass {}
interface UserData {}
type Status = 'active' | 'inactive';
enum Color { RED, GREEN, BLUE }
function doSomething() {}
const userName = 'Alice';
const MAX_COUNT = 100;
class Foo {
private readonly internalState = 5;
}file-name.ts.d.ts// Good
const message = 'Hello world';// Bad
const message = "Hello world";// Good
const greeting = `Hello ${name}`;
const multiline = `Line 1
Line 2`;String()Boolean()Number()// Good
const bool = Boolean(value);
const str = String(num);
const num = Number(str);enum SupportLevel { NONE, BASIC, ADVANCED }
// Bad
if (level) { }
// Good
if (level !== SupportLevel.NONE) { }constletvarreadonlyinterfacetypevarArray()Object()#thisreferences/language-features.mdreferences/type-system.mdreferences/naming-conventions.mdexamples/examples/class-example.tsexamples/function-example.tsexamples/types-example.ts