Is Protocol-Oriented Programming in TypeScript Possible?

Is Protocol-Oriented Programming in TypeScript Possible?

Adopt this popular Swift paradigm in your TypeScript code base

Protocol-Oriented Programming Is a Common Method of Abstraction in Swift

Protocol-oriented programming is a practice that many Swift programmers use to structure source code. It’s a common method of doing abstraction to help programmers model real-world concepts into computational objects for software development. Since abstraction is one of the concepts in object-oriented programming, could other programming languages have similar traits in their own way?

Before going straight to a conclusion, let’s first take a look at if we can implement a generic print() function for output in TypeScript just like with the CustomStringConvertible protocol in Swift.

TypeScript Interface Defines the Shape of an Object

Interface is a keyword that can be used to model computational objects with TypeScript. First, let’s define two interfaces (ICustomStringConvertible and ICustomDebugStringPrintable) just like a protocol declaration in Swift:

interface ICustomStringConvertible {
     description: String
    }

    interface ICustomDebugStringConvertible {
     debugDescription: String
    }

abstract interfaces typescript

What is Geek Coin

What is GeekCash, Geek Token

Best Visual Studio Code Themes of 2021

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

What is TypeScript? Why TypeScript? and Why Not TypeScript?

TypeScript extends JavaScript by adding Types. There are many great reasons to switch to TypeScript. Especially if your team uses JavaScript. There are some reasons to not use TypeScript as there are with any language or framework.

Design Patterns with Typescript: Interfaces vs Abstract classes

Contrast and compare interfaces and classes, while building “Universal sorter” class that can handle arrays, strings and linked lists. Before we begin — if the TypeScript syntax is new to you or if you need a quick reminder about setting types, be sure to check out the article A Walk Through Setting the Types in Typescript: Annotations vs Inference.

When to use `Abstract` Base Class vs `Interface`

When to use `Abstract` Base Class vs `Interface` - One defines an object’s characteristics, the other establishes a contract of what this object can do

Generics & Abstract Classes in TypeScript

In the above case the input and output are specified as type any which defeats the whole purpose of TypeScript as it will never find a type error if anything is allowed. Instead we could use generic syntax as denoted by <> to pass in a generic type when the function is called.

TypeScript Interfaces

Learn about interfaces in TypeScript