Achieving Type Safety Without Typescript
Álvaro José Agámez Licha
Senior Software Developer
TypeScript Slow Down Our Development
Reasons For Abandoning TypeScript
Deno: "Complicated and slow build process on each build of the Deno internal runtime code."
Svelte: "As a Svelte compiler developer, debugging without a build step greatly simplifies compiler development."
Using JSDoc for Type Safety
With JSDoc, you can enjoy the same benefits as TypeScript, while removing most of the TypeScript drawbacks.
Benefits Of Using JSDoc
- Retains JavaScript features (flexibility).
- Validates types.
- Generates .d.ts files.
- Development speed.
- Debugging speed.
- Code documentation generated.
Drawbacks Of Using JSDoc
- Can be more verbose.
- Learning curve (not complicated).
- Some complex TypeScript types can be tricky to implement.
- It's easy and not as fancy as TypeScript. ;).
Implementing JSDoc in Your Project
// tsconfig.json
{
"compilerOptions": {
"allowJs": true,
"checkJs": true
}
}
// VS Code Config
"javascript.validate.enable": true
JSDoc Syntax
There are 2 official sources to learn JSDoc syntax:
- JSDoc Official Documentation.
- TypeScript JSDoc extension reference.
Type Validation with JSDoc
To validate our JSDoc types we use the TypeScript compiler in the same way we do for TypeScript.
// package.json
"scripts": {
"build:types": "tsc -p tsconfig.build.types.json",
"check:types": "tsc"
}
Conclusion
JSDoc provides a viable alternative to TypeScript, allowing for type safety without sacrificing JavaScript's flexibility.
Type Checking And Type Safety Without Typescript
By Alvaro Agamez
Type Checking And Type Safety Without Typescript
- 114