Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/ngtools/webpack/src/transformers/ctor-parameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,13 @@ function typeReferenceToExpression(
case ts.SyntaxKind.NumberKeyword:
case ts.SyntaxKind.NumericLiteral:
return ts.createIdentifier('Number');
case ts.SyntaxKind.UnionType:
const childTypeNodes = (node as ts.UnionTypeNode).types.filter(t => t.kind !== ts.SyntaxKind.NullKeyword);

return childTypeNodes.length === 1
? typeReferenceToExpression(entityNameToExpression, childTypeNodes[0], typeChecker)
: undefined;

case ts.SyntaxKind.TypeReference:
const typeRef = node as ts.TypeReferenceNode;
let typeSymbol = typeChecker.getSymbolAtLocation(typeRef.typeName);
Expand Down
37 changes: 37 additions & 0 deletions packages/ngtools/webpack/src/transformers/ctor-parameters_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ function transform(input: string, additionalFiles?: Record<string, string>) {
return result;
}

// tslint:disable-next-line: no-big-function
describe('Constructor Parameter Transformer', () => {
it('records class name in same module', () => {
const input = `
Expand Down Expand Up @@ -211,4 +212,40 @@ describe('Constructor Parameter Transformer', () => {

expect(tags.oneLine`${result}`).toEqual(tags.oneLine`${output}`);
});

it('should work with union type and nullable argument', () => {
const input = `
@Injectable()
export class ProvidedService {
constructor() { }
}

@Injectable()
export class LibService {
constructor(
@Optional() private service: ProvidedService | null,
) {
}
}
`;

const output = `
import { __decorate, __param } from "tslib";

let ProvidedService = class ProvidedService { constructor() { } };
ProvidedService = __decorate([ Injectable() ], ProvidedService);
export { ProvidedService };

let LibService = class LibService {
constructor(service) { this.service = service; }
};
LibService.ctorParameters = () => [ { type: ProvidedService, decorators: [{ type: Optional }] } ];
LibService = __decorate([ Injectable(), __param(0, Optional()) ], LibService);
export { LibService };
`;

const result = transform(input);

expect(tags.oneLine`${result}`).toEqual(tags.oneLine`${output}`);
});
});