Programming Languages

TypeScript: JavaScript dengan Type Safety

TypeScript adalah superset of JavaScript yang adds static typing, developed by Microsoft. Compiles ke clean JavaScript, runs anywhere JavaScript runs. Why TypeScript: catch errors at compile time (before runtime), better IDE support (autocomplete, refactoring), self-documenting code (types serve as inline documentation), scales better untuk large codebases, easier refactoring dengan confidence. Type system: basic types (string, number, boolean, null, undefined), arrays, tuples, enums, any (escape hatch), unknown (type-safe any), void, never. Type annotations: variables, function parameters, return types. Type inference: TypeScript infers types when not explicitly declared. Interfaces: define object shapes, contracts untuk classes, can extend multiple interfaces. Type aliases: create custom types, union types (string | number), intersection types. Generics: create reusable components dengan type parameters, generic functions, classes, interfaces. Advanced types: conditional types, mapped types, utility types (Partial, Required, Pick, Omit, Record). Classes: full OOP support, access modifiers (public, private, protected), abstract classes, implements interfaces. Decorators: metadata untuk classes/methods, used heavily dalam frameworks (Angular, NestJS). Modules: ES6 module syntax, namespace organization. Configuration: tsconfig.json controls compiler options, strict mode recommended (enables all strict checks), target (ES5, ES2015, etc), module system, include/exclude files. Tooling: works seamlessly dengan VS Code (built by same team), integrates dengan Webpack, Rollup, Babel, ESLint dengan typescript-eslint. Migration strategy: rename .js files ke .ts gradually, start dengan loose types (any), progressively strengthen types, use @ts-ignore sparingly untuk legacy code. Frameworks adopting TypeScript: Angular (built with TS), Vue 3 (rewritten in TS), React (excellent TS support), NestJS (Node.js framework), Next.js. Type definitions: DefinitelyTyped repository (@types packages) provides types untuk JavaScript libraries, install dengan npm i @types/library-name. Debugging: source maps enable debugging TS directly, browser DevTools support. Best practices: enable strict mode, avoid any (use unknown instead), prefer interfaces over type aliases untuk object shapes, use const assertions untuk literal types, leverage utility types. Common pitfalls: over-typing (keep it simple), type assertions (as) should be rare, avoid enum if const object works. Performance: no runtime overhead (types erased during compilation), compilation time can increase untuk large projects (use project references). Alternatives: Flow (Facebook), JSDoc comments (type checking tanpa compilation). Community: large ecosystem, extensive documentation, active development. Learning curve: moderate untuk JavaScript developers, benefits increase dengan project size. TypeScript adoption growing rapidly: used by Microsoft, Google, Airbnb, Slack, majority new projects. Career advantage: TypeScript skills highly valued, salary premium 10-15% over JavaScript-only developers. TypeScript brings sanity ke JavaScript development, especially critical untuk team collaboration dan long-term maintainability.

Kembali ke Artikel

Butuh Solusi IoT atau Smart Sensor?

Tim ahli teknis kami siap memberikan konsultasi gratis untuk proyek Anda.

Hubungi Kami