TypeScript dosyasının tüm elementlerini gösteren template
#!/usr/bin/env bun
// @ts-nocheck
/**
* src/telegram/_template.ts
*
* WHY: TypeScript dosyasının tüm elementlerini gösteren template
* PATTERNS:
* - JSDoc documentation (WHY, PATTERNS, CONSTRAINTS)
* - Import statements (named, default, type, namespace, relative, path alias)
* - Type definitions (type alias, interface, discriminated union, generic, mapped)
* - Constants (string, number, object, array, exported, internal)
* - Variables (let, const, destructured, array destructured)
* - Functions (declaration, arrow, async, exported, internal, expression)
* - Classes (class, methods, properties, constructor)
* - Enums (enum, string enum)
* - Namespaces (namespace)
*
* CONSTRAINTS:
* - Yukarıdan aşağıya sıralı
* - Her bölüm optional (örnekler için hepsi dahil)
*/
// ============================================================================
// 1. IMPORTS
// ============================================================================
// Named imports
import { functionA, functionB } from '@libs/module-a'
import { type TypeA, type TypeB } from '@libs/types'
// Default import
import defaultExport from '@libs/default-module'
// Type-only import
import type { TypeOnly } from '@libs/type-module'
// Namespace import
import * as NamespaceModule from '@libs/namespace-module'
// Relative imports
import { localFunction } from './local-module'
import type { LocalType } from './local-types'
// Path alias imports
import { aliasFunction } from '@telegram/capabilities/module'
// External library imports
import { Bot } from 'grammy'
import type { Context } from 'grammy/types'
// ============================================================================
// 2. TYPE DEFINITIONS
// ============================================================================
// Type alias
type MyType = {
id: string
name: string
age: number
}
// Discriminated union
type Result<T> =
| { type: 'success'; data: T }
| { type: 'failed'; reason: string }
// Generic type
type Container<T> = {
value: T
metadata: {
created: Date
updated: Date
}
}
// Mapped type
type Readonly<T> = {
readonly [K in keyof T]: T[K]
}
// Interface
interface MyInterface {
id: string
name: string
method(): void
}
// Interface extending another
interface ExtendedInterface extends MyInterface {
extra: string
}
// ============================================================================
// 3. CONSTANTS
// ============================================================================
// String constant (exported)
export const EXPORTED_STRING = 'value' as const
// Number constant (exported)
export const EXPORTED_NUMBER = 42 as const
// Object constant (exported)
export const EXPORTED_CONFIG = {
timeout: 30000,
retries: 3,
enabled: true
} as const
// Array constant (exported)
export const EXPORTED_ARRAY = ['a', 'b', 'c'] as const
// Internal string constant
const INTERNAL_STRING = 'internal-value' as const
// Internal number constant
const INTERNAL_NUMBER = 100 as const
// Internal object constant
const INTERNAL_CONFIG = {
debug: false,
version: '1.0.0'
} as const
// ============================================================================
// 4. VARIABLES
// ============================================================================
// Let declaration (mutable)
let mutableCounter = 0
// Const declaration (computed)
const computedValue = Math.random() * 100
// Destructured object
const { id, name, age } = {
id: '123',
name: 'John',
age: 30
}
// Array destructured
const [first, second, ...rest] = [1, 2, 3, 4, 5]
// Nested destructuring
const {
user: { id: userId, name: userName },
settings: { theme, language }
} = {
user: { id: '123', name: 'John' },
settings: { theme: 'dark', language: 'tr' }
}
// ============================================================================
// 5. FUNCTIONS
// ============================================================================
// Exported function declaration
export function exportedFunction(params: { id: string }): string {
return `ID: ${params.id}`
}
// Internal function declaration
function internalFunction(params: { value: number }): number {
return params.value * 2
}
// Async function (exported)
export async function asyncExportedFunction(params: {
url: string
}): Promise<string> {
const response = await fetch(params.url)
return response.text()
}
// Async function (internal)
async function asyncInternalFunction(params: { delay: number }): Promise<void> {
await new Promise(resolve => setTimeout(resolve, params.delay))
}
// Arrow function (exported)
export const exportedArrowFunction = (params: { x: number; y: number }): number => {
return params.x + params.y
}
// Arrow function (internal)
const internalArrowFunction = (params: { text: string }): string => {
return params.text.toUpperCase()
}
// Function expression
const functionExpression = function(params: { name: string }): string {
return `Hello, ${params.name}`
}
// Arrow function with async
const asyncArrowFunction = async (params: { id: string }): Promise<void> => {
console.log(`Processing ${params.id}`)
}
// Function with generic
function genericFunction<T>(params: { value: T }): T {
return params.value
}
// Function with discriminated union return
function resultFunction(params: {
success: boolean
data?: string
}): Result<string> {
if (params.success) {
return { type: 'success', data: params.data || 'default' }
}
return { type: 'failed', reason: 'Operation failed' }
}
// ============================================================================
// 6. CLASSES (Optional)
// ============================================================================
// Class declaration (exported)
export class ExportedClass {
// Property
public property: string
// Private property
private privateProperty: number
// Constructor
constructor(params: { property: string; privateProperty: number }) {
this.property = params.property
this.privateProperty = params.privateProperty
}
// Public method
public publicMethod(): string {
return this.property
}
// Private method
private privateMethod(): number {
return this.privateProperty
}
// Static method
static staticMethod(): string {
return 'static'
}
}
// Class with inheritance
class ExtendedClass extends ExportedClass {
// Additional property
public extra: string
constructor(params: {
property: string
privateProperty: number
extra: string
}) {
super(params)
this.extra = params.extra
}
// Override method
public publicMethod(): string {
return `${super.publicMethod()} + ${this.extra}`
}
}
// ============================================================================
// 7. ENUMS (Optional)
// ============================================================================
// Enum declaration
export enum Status {
Pending = 'pending',
Active = 'active',
Completed = 'completed'
}
// Numeric enum
enum Priority {
Low, // 0
Medium, // 1
High // 2
}
// String enum
enum Language {
Turkish = 'tr',
English = 'en',
Spanish = 'es'
}
// ============================================================================
// 8. NAMESPACES (Optional)
// ============================================================================
// Namespace declaration
export namespace MyNamespace {
export const constant = 'namespace-constant'
export function namespaceFunction(): string {
return 'namespace-function'
}
export type NamespaceType = {
value: string
}
}
// ============================================================================
// 9. MAIN FUNCTION / ENTRY POINT
// ============================================================================
async function main(): Promise<void> {
// Example usage of all elements
// Use constants
console.log(EXPORTED_STRING, INTERNAL_STRING)
// Use variables
mutableCounter++
console.log(computedValue, first, second)
// Use functions
const result1 = exportedFunction({ id: '123' })
const result2 = await asyncExportedFunction({ url: 'https://example.com' })
const result3 = exportedArrowFunction({ x: 10, y: 20 })
// Use classes
const instance = new ExportedClass({
property: 'test',
privateProperty: 42
})
console.log(instance.publicMethod())
// Use enums
const status = Status.Active
const priority = Priority.High
const lang = Language.Turkish
// Use namespaces
const nsValue = MyNamespace.constant
const nsResult = MyNamespace.namespaceFunction()
console.log({
result1,
result2,
result3,
status,
priority,
lang,
nsValue,
nsResult
})
}
// Execute if run directly
if (import.meta.main) {
main().catch(console.error)
}Last updated
Was this helpful?