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
14 changes: 11 additions & 3 deletions examples/html.ts
Original file line number Diff line number Diff line change
@@ -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<
Expand All @@ -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<P<"This is test">>;
// type B = Html<Body<Div<[P<"Hello World">, P<"This is test">]>>>;
type ComplexHtml = Html<
Body<
[
Div<[P<'First paragraph'>, P<'Second paragraph'>]>,
P<'test'>,
Div<[P<'test1'>, Div<P<'test2'>>]>,
]
>
>;
6 changes: 5 additions & 1 deletion src/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,20 @@ type InvalidDivContent<T> = {
__invalidType: T;
};

declare const pBrand: unique symbol;
declare const divBrand: unique symbol;

export type Div<T extends HTMLElement[] | HTMLElement> = T extends Html<HTMLElement | HTMLElement[]>
? InvalidDivContent<T>
: T extends Body<HTMLElement | HTMLElement[]>
? InvalidDivContent<T>
: T extends HTMLElement[]
? {
[divBrand]: 'div';
children: T;
}
: T extends HTMLElement
? {
[divBrand]: 'div';
children: T;
}
: never;
Expand All @@ -71,6 +73,8 @@ type InvalidPContent<T> = {
__invalidType: T;
};

declare const pBrand: unique symbol;

export type P<T extends HTMLElement[] | HTMLElement> = T extends Div<HTMLElement | HTMLElement[]>
? InvalidPContent<T>
: T extends Html<HTMLElement | HTMLElement[]>
Expand Down
9 changes: 6 additions & 3 deletions src/tsUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) &&
Expand Down Expand Up @@ -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');
Expand Down