


Creating Reusable Base Classes in TypeScript with a Real-Life Example
This article delves into creating reusable base classes in TypeScript, expanding on a previous post about TypeScript conversion. While this post stands alone, the linked post offers valuable insights into codebase rewriting and unit testing.
Johnny-Five, an IoT and robotics library for Node.js, simplifies hardware interaction using a jQuery-like approach: normalizing differences across platforms and providing higher-level abstractions. It supports various platforms through IO Plugins, including Arduino, Tessel, and Raspberry Pi. Raspi IO, a plugin supporting Raspberry Pi, was rewritten in TypeScript, leading to the creation of a reusable base class.
Designing a Reusable Base Class
For those unfamiliar with class-based inheritance, a base class provides common structure and functionality that subclasses can extend. Consider this simplified TypeScript example:
abstract class Automobile { private _speed: number = 0; private _direction: number = 0; public drive(speed: number): void { this._speed = speed; } public turn(direction: number): void { if (direction > 90 || direction <p>This <code>Automobile</code> base class is extended to create a <code>Sedan</code> class. All methods from both classes are accessible. Similarly, an <code>AbstractIO</code> base class was created for Johnny-Five's IO Plugins.</p> <h3 id="Implementing-AbstractIO">Implementing AbstractIO</h3> <p><code>AbstractIO</code> is an npm package providing a base class conforming to both TypeScript and JavaScript best practices. Let's examine the <code>MODES</code> property and <code>digitalWrite</code> method:</p> <p>The <code>MODES</code> property is an object mapping mode names (INPUT, OUTPUT, etc.) to numerical constants. In TypeScript, an enum is ideal, but JavaScript lacks this feature. The solution? A TypeScript enum is used internally, mapped to a JavaScript object externally:</p> <pre class="brush:php;toolbar:false">export enum Mode { INPUT = 0, OUTPUT = 1, ANALOG = 2, PWM = 3, SERVO = 4, STEPPER = 5, UNKNOWN = 99 } public get MODES() { return { INPUT: Mode.INPUT, OUTPUT: Mode.OUTPUT, ANALOG: Mode.ANALOG, PWM: Mode.PWM, SERVO: Mode.SERVO, UNKNOWN: Mode.UNKNOWN }; }
For methods requiring subclass overrides (like digitalWrite
), a JavaScript approach was adopted: throwing an error in the base class:
public digitalWrite(pin: string | number, value: number): void { throw new Error(`digitalWrite is not supported by ${this.name}`); }
This works in both TypeScript and JavaScript, though TypeScript lacks warnings for un-overridden methods.
Key Learnings
The process of designing for both TypeScript and JavaScript highlighted these key points:
- Prioritize common best practices.
- If conflicts arise, use TypeScript best practices internally and map them to JavaScript externally.
- If neither works, default to JavaScript best practices.
Abstract classes proved less useful in JavaScript, and duck typing across modules presented maintenance challenges. Iterative feedback from library consumers was crucial for successful design.
Conclusion
Creating base classes for both TypeScript and JavaScript users is achievable, though some compromises are necessary. AbstractIO
successfully serves both communities, demonstrating that a balance can be struck.
The above is the detailed content of Creating Reusable Base Classes in TypeScript with a Real-Life Example. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics





It's out! Congrats to the Vue team for getting it done, I know it was a massive effort and a long time coming. All new docs, as well.

With the recent climb of Bitcoin’s price over 20k $USD, and to it recently breaking 30k, I thought it’s worth taking a deep dive back into creating Ethereum

I had someone write in with this very legit question. Lea just blogged about how you can get valid CSS properties themselves from the browser. That's like this.

I'd say "website" fits better than "mobile app" but I like this framing from Max Lynch:

The other day, I spotted this particularly lovely bit from Corey Ginnivan’s website where a collection of cards stack on top of one another as you scroll.

If we need to show documentation to the user directly in the WordPress editor, what is the best way to do it?

There are a number of these desktop apps where the goal is showing your site at different dimensions all at the same time. So you can, for example, be writing

Questions about purple slash areas in Flex layouts When using Flex layouts, you may encounter some confusing phenomena, such as in the developer tools (d...
