Skip to content

Commit bc090cb

Browse files
Added alias type support
1 parent a0e2463 commit bc090cb

File tree

5 files changed

+96
-10
lines changed

5 files changed

+96
-10
lines changed
Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,59 @@
1+
/**
2+
* Examples of features added in TypeScript 1.4.
3+
*
4+
* @see http://blogs.msdn.com/b/typescript/archive/2014/11/18/what-s-new-in-the-typescript-type-system.aspx
5+
*/
6+
7+
/**
8+
* A simple interface holding a member with an union type.
9+
*/
110
interface RunOptions {
211
program: string;
312
commandline: string[]|string;
413
}
514

15+
16+
/**
17+
* A type alias describing an array.
18+
*/
619
type PrimitiveArray = Array<string|number|boolean>;
20+
21+
22+
/**
23+
* A type alias describing a primitive value.
24+
*/
725
type MyNumber = number;
26+
27+
28+
/**
29+
* A type alias describing a reference type.
30+
*/
831
type MyRunOptions = RunOptions;
9-
type Callback = () => void;
1032

33+
34+
/**
35+
* A type alias of for a callback function.
36+
*
37+
* @param Callback.parameters The rest parameter.
38+
*/
39+
type Callback = (...parameters:string[]) => string;
40+
41+
42+
/**
43+
* A variable defined using an union type.
44+
*/
45+
var interfaceOrString:RunOptions|string;
46+
47+
48+
/**
49+
* A variable pointing to a type alias.
50+
*/
1151
var callback:Callback;
1252

13-
function functionUsingTypes(data:PrimitiveArray, callback:Callback):MyNumber {
53+
54+
/**
55+
* A function that has parameters pointing to type aliases and returns a type alias.
56+
*/
57+
function functionUsingTypes(aliasData:PrimitiveArray, callback:Callback):MyNumber {
1458
return 10;
1559
}

src/td/converter/Converter.ts

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,9 @@ module td
276276

277277

278278
function extractType(target:Reflection, node:ts.Node, type:ts.Type):Type {
279-
if (type.flags & ts.TypeFlags.Intrinsic) {
279+
if (node && node['typeName'] && node['typeName'].text && type && (!type.symbol || (node['typeName'].text != type.symbol.name))) {
280+
return new ReferenceType(node['typeName'].text, null, target.findReflectionByName(node['typeName'].text));
281+
} else if (type.flags & ts.TypeFlags.Intrinsic) {
280282
return extractIntrinsicType(<ts.IntrinsicType>type);
281283
} else if (type.flags & ts.TypeFlags.Enum) {
282284
return extractEnumType(type);
@@ -308,19 +310,31 @@ module td
308310

309311
function extractTupleType(target:Reflection, node:ts.TupleTypeNode, type:ts.TupleType):Type {
310312
var elements = [];
311-
node.elementTypes.forEach((elementNode) => {
312-
elements.push(extractType(target, elementNode, checker.getTypeAtLocation(elementNode)));
313-
});
313+
if (node.elementTypes) {
314+
node.elementTypes.forEach((elementNode:ts.TypeNode) => {
315+
elements.push(extractType(target, elementNode, checker.getTypeAtLocation(elementNode)));
316+
});
317+
} else {
318+
type.elementTypes.forEach((type:ts.Type) => {
319+
elements.push(extractType(target, null, type));
320+
});
321+
}
314322

315323
return new TupleType(elements);
316324
}
317325

318326

319327
function extractUnionType(target:Reflection, node:ts.UnionTypeNode, type:ts.UnionType):Type {
320328
var types = [];
321-
node.types.forEach((typeNode:ts.TypeNode) => {
322-
types.push(extractType(target, typeNode, checker.getTypeAtLocation(typeNode)));
323-
});
329+
if (node.types) {
330+
node.types.forEach((typeNode:ts.TypeNode) => {
331+
types.push(extractType(target, typeNode, checker.getTypeAtLocation(typeNode)));
332+
});
333+
} else {
334+
type.types.forEach((type:ts.Type) => {
335+
types.push(extractType(target, null, type));
336+
});
337+
}
324338

325339
return new UnionType(types);
326340
}
@@ -521,6 +535,7 @@ module td
521535
case ts.SyntaxKind.SetAccessor:
522536
return visitSetAccessorDeclaration(<ts.SignatureDeclaration>node, scope);
523537
case ts.SyntaxKind.CallSignature:
538+
case ts.SyntaxKind.FunctionType:
524539
return visitCallSignatureDeclaration(<ts.SignatureDeclaration>node, <DeclarationReflection>scope);
525540
case ts.SyntaxKind.IndexSignature:
526541
return visitIndexSignatureDeclaration(<ts.SignatureDeclaration>node, <DeclarationReflection>scope);
@@ -533,8 +548,10 @@ module td
533548
return visitTypeLiteral(<ts.TypeLiteralNode>node, scope);
534549
case ts.SyntaxKind.ExportAssignment:
535550
return visitExportAssignment(<ts.ExportAssignment>node, scope);
551+
case ts.SyntaxKind.TypeAliasDeclaration:
552+
return visitTypeAliasDeclaration(<ts.TypeAliasDeclaration>node, scope);
536553
default:
537-
// console.log('Unhandeled: ' + ts.SyntaxKind[node.kind]);
554+
// console.log('Unhandeled: ' + node.kind);
538555
return null;
539556
}
540557
}
@@ -1009,6 +1026,23 @@ module td
10091026
}
10101027

10111028

1029+
/**
1030+
* Analyze the given type alias declaration node and create a suitable reflection.
1031+
*
1032+
* @param node The type alias declaration node that should be analyzed.
1033+
* @param scope The reflection representing the current scope.
1034+
* @return The resulting reflection or NULL.
1035+
*/
1036+
function visitTypeAliasDeclaration(node:ts.TypeAliasDeclaration, scope:ContainerReflection):Reflection {
1037+
var alias = createDeclaration(scope, node, ReflectionKind.TypeAlias);
1038+
alias.type = extractType(alias, node.type, checker.getTypeAtLocation(node.type));
1039+
if (alias.name == 'Callback') {
1040+
// console.log(alias);
1041+
}
1042+
return alias;
1043+
}
1044+
1045+
10121046
function visitExportAssignment(node:ts.ExportAssignment, scope:ContainerReflection):Reflection {
10131047
var type = checker.getTypeAtLocation(node.exportName);
10141048
if (type && type.symbol) {

src/td/converter/plugins/GroupPlugin.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ module td
1818
ReflectionKind.EnumMember,
1919
ReflectionKind.Class,
2020
ReflectionKind.Interface,
21+
ReflectionKind.TypeAlias,
2122

2223
ReflectionKind.Constructor,
2324
ReflectionKind.Property,
@@ -56,6 +57,7 @@ module td
5657
plurals[ReflectionKind.Property] = 'Properties';
5758
plurals[ReflectionKind.Enum] = 'Enumerations';
5859
plurals[ReflectionKind.EnumMember] = 'Enumeration members';
60+
plurals[ReflectionKind.TypeAlias] = 'Type aliases';
5961
return plurals;
6062
})();
6163

src/td/converter/plugins/TypePlugin.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ module td
7878
for (var index = 0, count = tupleType.elements.length; index < count; index++) {
7979
resolveType(tupleType.elements[index]);
8080
}
81+
} else if (type instanceof UnionType) {
82+
var unionType:UnionType = <UnionType>type;
83+
for (var index = 0, count = unionType.types.length; index < count; index++) {
84+
resolveType(unionType.types[index]);
85+
}
8186
}
8287
}
8388
}

src/td/models/Reflection.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ module td
5252
GetSignature = 524288,
5353
SetSignature = 1048576,
5454
ObjectLiteral = 2097152,
55+
TypeAlias = 4194304,
5556

5657
ClassOrInterface = Class | Interface,
5758
VariableOrProperty = Variable | Property,

0 commit comments

Comments
 (0)