statto
August 8, 2018, 9:09pm
1
I’ve generated a DTO file and when I try to build the project a couple of errors are returned.
For export class GetAllConfigurations implements IReturn<Array<Configuration>>
the Type 'Array' is not generic
error is highlighted
For createResponse() { return new Array<Configuration>(); }
the Expected 0 type arguments, but got 1
is highlighted.
TS Lint is suggesting that Array is forbidden and should be Configuration[]
mythz
August 8, 2018, 9:13pm
2
What version of TypeScript are you using and is this an actual TypeScript compile error or just a TS Lint Error?
statto
August 8, 2018, 9:27pm
3
The version in package.json is “typescript”: “^3.0.1” and the error is a [ts] error which I think is a ts-loader error
mythz
August 8, 2018, 9:44pm
4
Please provide the C# DTO and the generated TypeScript DTO.
statto
August 9, 2018, 5:37am
5
C# Request class
[Route("/{OrgSlug}/configurations", Verbs = "GET")]
public class GetAllConfigurations : IReturn<Configuration[]>
{
public string OrgSlug { get; set; }
}
TypeScript Request class
// @Route("/{OrgSlug}/configurations", "GET")
export class GetAllConfigurations implements IReturn<Array<Configuration>>
{
orgSlug: string;
createResponse() { return new Array<Configuration>(); }
getTypeName() { return "GetAllConfigurations"; }
}
Array<Configuration>>
is highlighted with [ts] Type 'Array' is not generic
in VS Code and after doing npm run build
is returned as an error.
This is the same for all the classes that have an array type in IReturn in the dtos.ts file
mythz
August 9, 2018, 7:35am
6
Should be resolved with this commit which is available from v5.1.1 that’s now available on MyGet .
Although I can’t reproduce any issue with:
createResponse() { return new Array<Configuration>(); }
Which is valid running with TypeScript 3.0.1 or using the default TS Lint settings.
Using any combination of:
createResponse() { return new Configuration[]; }
createResponse() { return new Configuration[]{}; }
createResponse() { return Configuration[]; }
createResponse() { return Configuration[]{}; }
Results in syntax errors, the only valid syntax for returning generic arrays are:
createResponse() { return new Array<Configuration>(); }
createResponse() { return <Configuration[]>[]; }
createResponse() { return [] as Configuration[]; }
I’ve chosen to retain the original syntax which uses the least foreign syntax.
Incidentally generic arrays are fairly unbalanced all-round in TypeScript as you need to use short-hand syntax when implementing a generic array argument, e.g:
public class MyRequest implements IReturm<MyArray[]> { .. }
But need to use generic type syntax if you’re extending a generic Array, e.g:
public class MyRequest extends Array<MyArray> { .. }
statto
August 10, 2018, 8:26am
7
Thanks for the reply, will MyGet update typescript-ref as I generated the DTOs from this in VS Code
mythz
August 10, 2018, 9:33am
8
The DTO generation happens in your ServiceStack server libraries, i.e. The npm scripts that VS Code uses just calls your ServiceStack instance to generate the DTOs.
statto
August 10, 2018, 10:08am
9
OK I’ve tried that and the DTOs are the same, what difference should I see in the generated files?
mythz
August 10, 2018, 10:58am
10
The IReturn interface marker now generates interfaces using short-hand array literal syntax, e.g IReturn<Configuration[]>
.
Make sure you’re using the latest v5.1.1 on MyGet , if you were using v5.1.1 previously you will need to clear your NuGet cache, e.g:
$ nuget locals all -clear