question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItĀ collects links to all the places you might be looking at while hunting down a tough bug.

And, if youā€™re still stuck at the end, weā€™re happy to hop on a call to see how we can help out.

Type names from typescript type alias definitions

See original GitHub issue

To start off: great library šŸ˜ !!!

Question/issue about the naming of Schemas.

Sorting

  • Iā€™m submitting a ā€¦

    • bug report
    • feature request
    • support request
  • I confirm that I

    • used the search to make sure that a similar issue hasnā€™t already been submit

Expected Behavior

type LoginStatus = {
    loggedIn: boolean;
    username: string;
}

/**
 * A narrower LoginStatus
 */
type LoginStatusFailed = Pick<LoginStatus, 'loggedIn'>

@Route('api/v1/login')
export class LoginController extends Controller {

    @Path('/')
    @Post()
    @Response<LoginStatusFailed>(StatusCode.UNAUTHORIZED, "Unauthorized", { loggedIn: false })
    public async doLogin(...): Promise<LoginStatus> {
       this.setStatus(StatusCode.UNAUTHORIZED)
       return { loggedIn: false }
    }
}

Current Behavior

Expecting LoginStatusFailed instead of Pick_LoginStatus.loggedIn_.

Swagger UI image

Spec Schemas

"LoginStatus": {
    "properties": {
        "username": {
            "type": "string"
        },
        "loggedIn": {
            "type": "boolean"
        }
    },
    "required": [
        "loggedIn", "username"
    ],
    "type": "object"
},
"Pick_LoginStatus.loggedIn_": {
    "properties": {
        "loggedIn": {
            "type": "boolean"
        }
    },
    "required": [
        "loggedIn"
    ],
    "type": "object",
    "description": "From T, pick a set of properties whose keys are in the union K"
},
"LoginStatusFailed": {
    "$ref": "#/components/schemas/Pick_LoginStatus.loggedIn_",
    "description": "A narrower LoginStatus"
},
...

DoLogin controller This seems to be correct in swagger.json paths["/api/v1/login"].post.responses["401"].content["application/json"].schema.$ref.

"schema": {
    "$ref": "#/components/schemas/LoginStatusFailed"
},

Possible Solution

Steps to Reproduce

  1. Create type alias using Omit or Pick
  2. Make Controller method return type alias
  3. Expect type alias name to be the schema name

Context (Environment)

Version of the library: 3.3.0 Version of NodeJS: v12.18.3

Also using ā€œswagger-ui-expressā€ 4.1.4 to display spec.

Detailed Description

Would also be nice if From T, pick a set of properties whose keys are in the union K could be rendered with the proper types, not sure if achievable.

Breaking change?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:12 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
cakoosecommented, Oct 18, 2020

Re: breaking change. Yeah, this would have to be opt-in, maybe via a flag in ā€œtsoa.jsonā€.

Re: readability. Since OpenAPI currently canā€™t represent generic types, tsoa has to generate a mangled name. Are there cases where someone would actually want these types present in generated spec, e.g. Pick_LoginStatus.loggedIn_? Again, Iā€™m new to tsoa so maybe Iā€™m missing some use cases.

0reactions
liaojiwei666commented, May 10, 2022

any update? I had faced same problem when I follow the official tutorial image

Read more comments on GitHub >

github_iconTop Results From Across the Web

Documentation - Advanced Types - TypeScript
Type aliases create a new name for a type. Type aliases are sometimes similar to interfaces, but can name primitives, unions, tuples, and...
Read more >
3 Ways to Name Types in Typescript - geekAbyte
Type aliases are another mechanism for naming types in TypeScript. It is a feature that makes it possible to give a name (or...
Read more >
TypeScript | Type Aliases - Codecademy
In TypeScript, type aliases create type definitions, using the `type` keyword and a name, that can be reused throughout the code.
Read more >
What are type aliases and how to create it in Typescript
In Typescript, Type aliases give a type a new name. They are similar to interfaces in that they can be used to name...
Read more >
What are type aliases in TypeScript? - Educative.io
In TypeScript, type aliases are used to assign a name for any type. It doesn't create a new type. Instead, it provides a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found