Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mapped type: Record<'foo', string> #653

Closed
2 of 4 tasks
Eywek opened this issue Apr 8, 2020 · 2 comments · Fixed by #662
Closed
2 of 4 tasks

mapped type: Record<'foo', string> #653

Eywek opened this issue Apr 8, 2020 · 2 comments · Fixed by #662

Comments

@Eywek
Copy link
Contributor

Eywek commented Apr 8, 2020

I'm trying to use Record<'id', string>;.
I've seen #505 and it seems that it's known that Record<> aren't supported, but I wanted to have some update on this.

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

export interface TestModel {
  recordType?: Record<'id', string>;
}
There was a problem resolving type of 'Record<'id', string>'.
There was a problem resolving type of 'TestModel'.

/private/tmp/tsoa/src/metadataGeneration/typeResolver.ts:151
      const tsProperties = this.current.typeChecker.getPropertiesOfType(type).map(symbol => {
                                                                              ^
TypeError: Cannot read property '0' of undefined
    at /private/tmp/tsoa/src/metadataGeneration/typeResolver.ts:151

Thank you

@WoH
Copy link
Collaborator

WoH commented Apr 9, 2020

Record does not give me the properties, so I haven't had time to address this.
Similarly, mapped types that modify the type are not covered yet.
If someone has a good way to get that info back into TypeNodes we can resolve I'd be gladly taking suggestions.

@Eywek
Copy link
Contributor Author

Eywek commented Apr 15, 2020

I've found this (to try in the isMappedTypeNode scope):

	  const type = this.current.typeChecker.getTypeFromTypeNode(this.referencer);
      const typeChecker = this.current.typeChecker
      for (const property of type.getProperties()) {
        const propertyType = typeChecker.getTypeOfSymbolAtLocation(property, this.typeNode);
        console.log({
          name: property.name,
          declarationsCount: property.declarations && property.declarations.length,
          typeName: typeChecker.typeToString(propertyType),
          typeDeclarations: propertyType.symbol && propertyType.symbol.declarations && propertyType.symbol.declarations.length,
          typeResolved: new TypeResolver(this.current.typeChecker.typeToTypeNode(propertyType)!, this.current, this.typeNode, this.context, this.referencer).resolve(),
        })
      }

which prints this:

{
  name: 'record-foo',
  declarationsCount: undefined,
  typeName: '{ data: string; }',
  typeDeclarations: 1,
  typeResolved: {
    additionalProperties: undefined,
    dataType: 'nestedObjectLiteral',
    properties: [ [Object] ]
  }
}
{
  name: 'record-bar',
  declarationsCount: undefined,
  typeName: '{ data: string; }',
  typeDeclarations: 1,
  typeResolved: {
    additionalProperties: undefined,
    dataType: 'nestedObjectLiteral',
    properties: [ [Object] ]
  }
}

for:

class TestModel {
	record?: Record<'record-foo' | 'record-bar', { data: string }>
}

EDIT: I've been able to find a way to make things works, but it need some duplications, you can check here master...Kiwup:record
It works with Record<> and other mapped types

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants