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.
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.
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.
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.
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.
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.
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.
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
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" }
]
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With