Skip to content

Commit

Permalink
feat: include service and definition types with implementations (#552)
Browse files Browse the repository at this point in the history
When exporting a service or service definition, also export it as a
type. Previously, you would have to import the value then `typeof` off
of it which is cumbersome. Automate this as it has no runtime overhead
and seems more intuitive.

Before:

```ts
export const TestDefinition = {
  name: 'Test',
  fullName: 'simple.Test',
  methods: {
    …
  },
} as const;
```

After:

```ts
export type TestDefinition = typeof TestDefinition;
export const TestDefinition = {
  name: 'Test',
  fullName: 'simple.Test',
  methods: {
    …
  },
} as const;
```
  • Loading branch information
niedzielski committed May 1, 2022
1 parent ad5f800 commit 6b896f4
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 2 deletions.
29 changes: 29 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,32 @@
## [next](https://github.com/stephenh/ts-proto/compare/v1.110.4...main) (????-??-??)

### Features

* When outputing service and service definition implementations, include types. Eg, before:

```ts
export const TestDefinition = {
name: 'Test',
fullName: 'simple.Test',
methods: {
},
} as const;
```

Now:

```ts
export type TestDefinition = typeof TestDefinition;
export const TestDefinition = {
name: 'Test',
fullName: 'simple.Test',
methods: {
},
} as const;
```

## [1.110.4](https://github.com/stephenh/ts-proto/compare/v1.110.3...v1.110.4) (2022-04-08)


Expand Down
1 change: 1 addition & 0 deletions integration/generic-metadata/hero.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ export class HeroServiceClientImpl implements HeroService {
}
}

export type HeroServiceDefinition = typeof HeroServiceDefinition;
export const HeroServiceDefinition = {
name: 'HeroService',
fullName: 'hero.HeroService',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export const TestMessage = {
};

/** @deprecated */
export type TestDefinition = typeof TestDefinition;
export const TestDefinition = {
name: 'Test',
fullName: 'simple.Test',
Expand Down
1 change: 1 addition & 0 deletions integration/generic-service-definitions/simple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export const TestMessage = {
};

/** @deprecated */
export type TestDefinition = typeof TestDefinition;
export const TestDefinition = {
name: 'Test',
fullName: 'simple.Test',
Expand Down
1 change: 1 addition & 0 deletions integration/grpc-js/simple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ export const TestMessage = {
*
* @deprecated
*/
export type TestService = typeof TestService;
export const TestService = {
/**
* Unary
Expand Down
1 change: 1 addition & 0 deletions integration/use-date-true/use-date-true.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ export class ClockClientImpl implements Clock {
}
}

export type ClockDefinition = typeof ClockDefinition;
export const ClockDefinition = {
name: 'Clock',
fullName: 'Clock',
Expand Down
8 changes: 7 additions & 1 deletion src/generate-generic-service-definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,15 @@ export function generateGenericServiceDefinition(

maybeAddComment(sourceInfo, chunks, serviceDesc.options?.deprecated);

// Service definition type
const name = def(`${serviceDesc.name}Definition`);
chunks.push(code`
export type ${name} = typeof ${name};
`);

// Service definition
chunks.push(code`
export const ${def(`${serviceDesc.name}Definition`)} = {
export const ${name} = {
`);

serviceDesc.options?.uninterpretedOption;
Expand Down
8 changes: 7 additions & 1 deletion src/generate-grpc-js.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,15 @@ function generateServiceDefinition(

maybeAddComment(sourceInfo, chunks, serviceDesc.options?.deprecated);

// Service definition type
const name = def(`${serviceDesc.name}Service`);
chunks.push(code`
export type ${name} = typeof ${name};
`);

// Service definition
chunks.push(code`
export const ${def(`${serviceDesc.name}Service`)} = {
export const ${name} = {
`);

for (const [index, methodDesc] of serviceDesc.method.entries()) {
Expand Down

0 comments on commit 6b896f4

Please sign in to comment.