microsoft / microsoft/typespec

http-server-js: Add option to emit `service`s as `abstract class`

Open
#7,329 2 comments 0 reactions 0 assignees View on GitHub
design:needed emitter:service:js triaged:core
Dominant language
Java
Stars
5.9k
Forks
394
Avg merge
1d 23h
Merged PRs (30d)
104

Description

### `http-server-js`: Add option to emit `service`s as TypeScript `abstract class` instead of `interface`

For a TypeSpec `interface` declaration such as

```ts
@route("/widgets")
@tag("Widgets")
interface Widgets {
/** List widgets */
```

The `http-server-js` emitter currently emits a TypeScript `interface`:

```ts
export interface Widgets {
/**
```

However, I would like to have the option to emit an `abstract class` instead.

I can think of two main benefits of using an `abstract class`:
* The `override` keyword can be used, and it can be used to notice cases where a service implementation implements unnecessary methods (for example, those that were removed)
* The base class becomes injectable in runtime dependency injection frameworks [such as NestJS](https://github.com/nestjs/nest/issues/43). This is because `interface` types cannot be reified as values that can be inspected by injection frameworks

However, this may not make sense for all users, so it should be a setting, rather than a change of the default behavior.

I would suggesting adding a new emitter option such as `"service-interfaces": "interface" | "abstract class"`

### Checklist

- [x] Follow our [Code of Conduct](https://github.com/microsoft/typespec/blob/main/CODE_OF_CONDUCT.md)
- [x] Read the [docs](https://typespec.io/docs/).
- [x] Check that there isn't already an issue that request the same feature to avoid creating a duplicate.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.