Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript: Reuse of interfaces and classes for frontend (Angular) and backend

I have a monorepo for a current project based on a Frontend (Angular) and a Backend (developed with NestJS - so it's NodeJS). I want to use custom interfaces and classes for both - frontend and backend. For example, creating DTOs so my frontend knows the parameter of my backend.

I thought about a common folder like the following project structure shows, but this is not working, because the common folder is outside the scope of Angular (tsconfig) and so auto completition isn't working

project
├── client (Angular)
├── server (NestJS)
└── common (client and server share specific interfaces and classes)

Does anyone has experience for this? At the moment I add my interfaces to both folders, but this is is evil because if I update one interface, I have to replace the other, too.

like image 411
Paul Avatar asked Nov 04 '18 07:11

Paul


People also ask

Is TypeScript used for frontend or backend?

Is TypeScript Frontend or Backend? TypeScript is neither a frontend or backend language, but rather a superset of the already established and well-known software language, JavaScript.

Should I use classes or interfaces in TypeScript?

When should we use classes and interfaces? If you want to create and pass a type-checked class object, you should use TypeScript classes. If you need to work without creating an object, an interface is best for you.

What is the difference between TypeScript interface and class?

A class is a blueprint from which we can create objects that share the same configuration - properties and methods. An interface is a group of related properties and methods that describe an object, but neither provides implementation nor initialisation for them.

What is the difference between interface and class in typescript?

An interface is an abstract type, it does not contain any code as a class does. What is the difference between Interface and Class in TypeScript? A class is a blueprint from which we can create objects that share the same configuration — properties and methods.

How to define custom types in typescript?

The better approach to use Custom Types in TypeScript is by using Interfaces. An interface is a structure that defines the syntax for classes to follow. Along with functions, an interface can also be used with a Class as well to define custom types. An interface is an abstract type, it does not contain any code as a class does.

How to extend a class and reuse the code in angular?

If Angular components have similar or identical code, It is very easy to extend a class and reuse the code. This way we can easily avoid copy-pasting the same code block over and over in the components. Reuse the component code when they have common code and common functionality. Create a base component class and extend it to all other components.

What is the difference between ES6 classes and typescript classes?

ES6 Classes have nothing to do with type checking. Typescript Classes may or may not be used for type checking depending on if they are constructed using types. Typescript Interfaces have no other purpose than type checking – Randy Casburn Jan 24 '19 at 23:16 2 Possible duplicate of Difference between interfaces and classes in Typescript


Video Answer


1 Answers

In TypeScript 3.0 "Project references" were introduced. This could help you achieve what you want (I was using it to share models between angular and cloud function)

https://www.typescriptlang.org/docs/handbook/project-references.html

What you need to do is add an external project reference to the tsconfig.json of the project that should reuse files from somewhere else

{
  "compilerOptions": {
    // The usual config
  },
  "references": [
    { "path": "../my-other-project" }
  ]
}
like image 176
Vojtech Avatar answered Oct 06 '22 01:10

Vojtech