Loading...
Loading...
Enforce strict TypeScript standards including centralized type definitions in app/types/, arrow functions only, explicit return types, and lang="ts" in all components. Use when reviewing or creating TypeScript code.
npx skill4agent add neversight/skills_feed typescript-best-practicesapp/types/import type { User } from '~/types/user'lang="ts"<script setup lang="ts"><script setup>const myFunc = (): string => { ... }function myFunc() { ... }const getData = (): Promise<User[]> => { ... }const getData = async () => { ... }const items: Product[] = []const items = []app/types/app/
types/
user.ts # User-related types
product.ts # Product types
api.ts # API response types
forms.ts # Form data types
state.ts # State management types
common.ts # Shared/utility types
errors.ts # Error types
index.ts # Optional re-exportsuser.tsproduct.tsapi.tsforms.tsstate.ts// ✅ CORRECT
<script setup lang="ts">
import type { User, Product } from '~/types'
interface Props {
user: User
items: Product[]
}
const props = defineProps<Props>()
const formatName = (user: User): string => {
return `${user.firstName} ${user.lastName}`
}
</script>// ✅ CORRECT
// app/composables/useData.ts
import type { User, ApiResponse } from '~/types'
export const useData = () => {
const data = ref<User | null>(null)
const fetchData = async (): Promise<User> => {
const { data: response } = await useFetch<ApiResponse<User>>('/api/user')
if (!response.value) throw new Error('No data')
return response.value.data
}
return { data, fetchData }
}// ✅ CORRECT
// app/stores/user.ts
import type { User, LoginCredentials } from '~/types'
export const useUserStore = defineStore('user', () => {
const user = ref<User | null>(null)
const login = async (creds: LoginCredentials): Promise<void> => {
// Implementation
}
return { user: readonly(user), login }
})// ✅ CORRECT
// app/utils/formatters.ts
import type { User, Product } from '~/types'
export const formatUser = (user: User): string => {
return `${user.firstName} ${user.lastName}`
}
export const calculateTotal = (products: Product[]): number => {
return products.reduce((sum: number, p: Product): number =>
sum + p.price, 0
)
}// app/types/user.ts
export interface User {
id: string
email: string
firstName: string
lastName: string
role: UserRole
createdAt: Date
}
export type UserRole = 'admin' | 'user' | 'guest'// app/types/api.ts
export interface ApiResponse<T> {
data: T
message: string
success: boolean
}
export interface PaginatedResponse<T> {
items: T[]
total: number
page: number
}// app/types/forms.ts
export interface LoginForm {
email: string
password: string
}
export interface FormField<T> {
value: T
error: string | null
touched: boolean
}// app/types/state.ts
export interface LoadingState {
isLoading: boolean
error: Error | null
}
export interface DataState<T> extends LoadingState {
data: T | null
}<!-- WRONG -->
<script setup lang="ts">
interface User { // ❌ Type defined inline
id: string
name: string
}
</script>app/types/user.ts// WRONG
const getData = async () => { // ❌ No return type
return data
}// CORRECT
const getData = async (): Promise<Data> => {
return data
}// WRONG
function handleClick() { // ❌ function keyword
// ...
}// CORRECT
const handleClick = (): void => {
// ...
}<!-- WRONG -->
<script setup> <!-- ❌ No lang="ts" -->
const data = ref()
</script><!-- CORRECT -->
<script setup lang="ts">
const data = ref<string>('')
</script>// WRONG
const items = [] // ❌ Implicit any[]
const user = ref() // ❌ Implicit any// CORRECT
const items: string[] = []
const user = ref<User | null>(null)app/types/lang="ts"functionanyunknownimport typetsconfig.json{
"compilerOptions": {
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true
}
}app/types/lang="ts"import typeunknownanyfunctionanyanylang="ts"lang="ts"app/types/user.tslang="ts"import type { User } from '~/types'