diff --git a/examples/html.ts b/examples/html.ts index 2e81765..519fbd3 100644 --- a/examples/html.ts +++ b/examples/html.ts @@ -1,4 +1,4 @@ -import type { Html, P } from '../src/html'; +import type { Body, Div, Html, P } from '../src/html'; // type A = HTML<"aaaa">; // type B = Html< @@ -11,6 +11,14 @@ import type { Html, P } from '../src/html'; /** * entrypoint */ -type B = Html<[P<'Hello World'>, P<'This is test'>]>; +// type B = Html<[P<'Hello World'>, P<'This is test'>]>; // type B = Html>; -// type B = Html, P<"This is test">]>>>; +type ComplexHtml = Html< + Body< + [ + Div<[P<'First paragraph'>, P<'Second paragraph'>]>, + P<'test'>, + Div<[P<'test1'>, Div>]>, + ] + > +>; diff --git a/src/html.ts b/src/html.ts index 03fed42..b585018 100644 --- a/src/html.ts +++ b/src/html.ts @@ -50,7 +50,7 @@ type InvalidDivContent = { __invalidType: T; }; -declare const pBrand: unique symbol; +declare const divBrand: unique symbol; export type Div = T extends Html ? InvalidDivContent @@ -58,10 +58,12 @@ export type Div = T extends Html : T extends HTMLElement[] ? { + [divBrand]: 'div'; children: T; } : T extends HTMLElement ? { + [divBrand]: 'div'; children: T; } : never; @@ -71,6 +73,8 @@ type InvalidPContent = { __invalidType: T; }; +declare const pBrand: unique symbol; + export type P = T extends Div ? InvalidPContent : T extends Html diff --git a/src/tsUtil.ts b/src/tsUtil.ts index b1a59d1..dae9529 100644 --- a/src/tsUtil.ts +++ b/src/tsUtil.ts @@ -15,7 +15,6 @@ export function traverseNode(node: ts.Node, indent: number = 0): HtmlJson { ts.isIdentifierOrThisTypeNode(member.name) && ts.isPropertySignature(member) ) { - console.log(member.name.escapedText); if ( member.type && ts.isLiteralTypeNode(member.type) && @@ -65,9 +64,13 @@ export function visit(node: ts.Node, checker: ts.TypeChecker, outPath: string) { ) { try { const type = checker.getTypeAtLocation(node); - const stringJSON = checker.typeToTypeNode(type, undefined, undefined); + const stringJSON = checker.typeToTypeNode( + type, + undefined, + ts.NodeBuilderFlags.InTypeAlias | ts.NodeBuilderFlags.NoTruncation + ); if (stringJSON) { - const result = traverseNode(stringJSON); + const result = traverseNode(stringJSON, 0); const writeStream = createWriteStream(outPath, { flags: 'w' }); renderToStream(result, writeStream); writeStream.end('\n');