@@ -61,7 +61,7 @@ export type ClassMap = Map<string, Class>;
6161 */
6262export class Class {
6363 private _comment ?: string ;
64- private _typedef ?: string ;
64+ private _typedefs : TypeNode [ ] = [ ] ;
6565 private readonly children : Class [ ] = [ ] ;
6666 private readonly _parents : Class [ ] = [ ] ;
6767 private readonly _props : Set < Property > = new Set ( ) ;
@@ -97,11 +97,15 @@ export class Class {
9797 return appendLine ( this . _comment , deprecated ) ;
9898 }
9999
100- protected get typedefs ( ) : string [ ] {
100+ protected get typedefs ( ) : TypeNode [ ] {
101+ const f : TypeNode = undefined ! ;
102+
101103 const parents = this . allParents ( ) . flatMap ( p => p . typedefs ) ;
102104 return Array . from (
103- new Set ( this . _typedef ? [ this . _typedef , ...parents ] : parents )
104- ) . sort ( ) ;
105+ new Map ( [ ...this . _typedefs , ...parents ] . map ( t => [ JSON . stringify ( t ) , t ] ) )
106+ )
107+ . sort ( ( [ key1 , value1 ] , [ key2 , value2 ] ) => key1 . localeCompare ( key2 ) )
108+ . map ( ( [ _ , value ] ) => value ) ;
105109 }
106110
107111 private properties ( ) {
@@ -198,13 +202,8 @@ export class Class {
198202 return false ;
199203 }
200204
201- addTypedef ( typedef : string ) {
202- if ( this . _typedef ) {
203- throw new Error (
204- `Class ${ this . subject . href } already has typedef ${ this . _typedef } but we're also adding ${ typedef } `
205- ) ;
206- }
207- this . _typedef = typedef ;
205+ addTypedef ( typedef : TypeNode ) {
206+ this . _typedefs . push ( typedef ) ;
208207 }
209208
210209 addProp ( p : Property ) {
@@ -299,7 +298,7 @@ export class Class {
299298
300299 protected nonEnumType ( skipDeprecated : boolean ) : TypeNode [ ] {
301300 this . children . sort ( ( a , b ) => CompareKeys ( a . subject , b . subject ) ) ;
302- const children = this . children
301+ const children : TypeNode [ ] = this . children
303302 . filter ( child => ! ( child . deprecated && skipDeprecated ) )
304303 . map ( child =>
305304 factory . createTypeReferenceNode (
@@ -309,11 +308,7 @@ export class Class {
309308 ) ;
310309
311310 // A type can have a valid typedef, add that if so.
312- children . push (
313- ...this . typedefs . map ( t =>
314- factory . createTypeReferenceNode ( t , /*typeArgs=*/ [ ] )
315- )
316- ) ;
311+ children . push ( ...this . typedefs ) ;
317312
318313 const upRef = this . leafName ( ) || this . baseName ( ) ;
319314 return upRef
@@ -379,9 +374,25 @@ export class Builtin extends Class {}
379374 * in JSON-LD and JavaScript as a typedef to a native type.
380375 */
381376export class AliasBuiltin extends Builtin {
382- constructor ( url : string , equivTo : string ) {
377+ constructor ( url : string , ... equivTo : TypeNode [ ] ) {
383378 super ( UrlNode . Parse ( url ) ) ;
384- this . addTypedef ( equivTo ) ;
379+ for ( const t of equivTo ) this . addTypedef ( t ) ;
380+ }
381+
382+ static Alias ( equivTo : string ) : TypeNode {
383+ return factory . createTypeReferenceNode ( equivTo , /*typeArgs=*/ [ ] ) ;
384+ }
385+
386+ static NumberStringLiteral ( ) : TypeNode {
387+ return factory . createTemplateLiteralType (
388+ factory . createTemplateHead ( /* text= */ '' ) ,
389+ [
390+ factory . createTemplateLiteralTypeSpan (
391+ factory . createTypeReferenceNode ( 'number' ) ,
392+ factory . createTemplateTail ( /* text= */ '' )
393+ ) ,
394+ ]
395+ ) ;
385396 }
386397}
387398
0 commit comments