Table of Contents
Designing a Reusable Base Class
Implementing AbstractIO
Key Learnings
Conclusion
Home Web Front-end CSS Tutorial Creating Reusable Base Classes in TypeScript with a Real-Life Example

Creating Reusable Base Classes in TypeScript with a Real-Life Example

Apr 21, 2025 am 09:31 AM

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
  };
}
Copy after login

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}`);
}
Copy after login

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:

  1. Prioritize common best practices.
  2. If conflicts arise, use TypeScript best practices internally and map them to JavaScript externally.
  3. 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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Vue 3 Vue 3 Apr 02, 2025 pm 06:32 PM

It&#039;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.

Building an Ethereum app using Redwood.js and Fauna Building an Ethereum app using Redwood.js and Fauna Mar 28, 2025 am 09:18 AM

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

Can you get valid CSS property values from the browser? Can you get valid CSS property values from the browser? Apr 02, 2025 pm 06:17 PM

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&#039;s like this.

A bit on ci/cd A bit on ci/cd Apr 02, 2025 pm 06:21 PM

I&#039;d say "website" fits better than "mobile app" but I like this framing from Max Lynch:

Stacked Cards with Sticky Positioning and a Dash of Sass Stacked Cards with Sticky Positioning and a Dash of Sass Apr 03, 2025 am 10:30 AM

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.

Using Markdown and Localization in the WordPress Block Editor Using Markdown and Localization in the WordPress Block Editor Apr 02, 2025 am 04:27 AM

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

Comparing Browsers for Responsive Design Comparing Browsers for Responsive Design Apr 02, 2025 pm 06:25 PM

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

Why are the purple slashed areas in the Flex layout mistakenly considered 'overflow space'? Why are the purple slashed areas in the Flex layout mistakenly considered 'overflow space'? Apr 05, 2025 pm 05:51 PM

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...

See all articles