Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support inferred types in conditionals (#1265)
- Loading branch information
Showing
30 changed files
with
595 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import ts from "typescript"; | ||
import { Context, NodeParser } from "../NodeParser"; | ||
import { SubNodeParser } from "../SubNodeParser"; | ||
import { BaseType } from "../Type/BaseType"; | ||
import { InferType } from "../Type/InferType"; | ||
|
||
export class InferTypeNodeParser implements SubNodeParser { | ||
public constructor(protected typeChecker: ts.TypeChecker, protected childNodeParser: NodeParser) {} | ||
|
||
public supportsNode(node: ts.InferTypeNode): boolean { | ||
return node.kind === ts.SyntaxKind.InferType; | ||
} | ||
|
||
public createType(node: ts.InferTypeNode, _context: Context): BaseType | undefined { | ||
return new InferType(node.typeParameter.name.escapedText.toString()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { BaseType } from "./BaseType"; | ||
|
||
export class InferType extends BaseType { | ||
constructor(private id: string) { | ||
super(); | ||
} | ||
|
||
public getId(): string { | ||
return this.id; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,40 @@ | ||
import { derefType } from "../Utils/derefType"; | ||
import { ArrayType } from "./ArrayType"; | ||
import { BaseType } from "./BaseType"; | ||
import { InferType } from "./InferType"; | ||
import { RestType } from "./RestType"; | ||
|
||
function normalize(types: Readonly<Array<BaseType | undefined>>): Array<BaseType | undefined> { | ||
let normalized: Array<BaseType | undefined> = []; | ||
|
||
for (const type of types) { | ||
if (type instanceof RestType) { | ||
const inner_type = derefType(type.getType()) as ArrayType | InferType | TupleType; | ||
normalized = [ | ||
...normalized, | ||
...(inner_type instanceof TupleType ? normalize(inner_type.getTypes()) : [type]), | ||
]; | ||
} else { | ||
normalized.push(type); | ||
} | ||
} | ||
return normalized; | ||
} | ||
|
||
export class TupleType extends BaseType { | ||
public constructor(private types: readonly (BaseType | undefined)[]) { | ||
private types: Readonly<Array<BaseType | undefined>>; | ||
|
||
public constructor(types: Readonly<Array<BaseType | undefined>>) { | ||
super(); | ||
|
||
this.types = normalize(types); | ||
} | ||
|
||
public getId(): string { | ||
return `[${this.types.map((item) => item?.getId() ?? "never").join(",")}]`; | ||
} | ||
|
||
public getTypes(): readonly (BaseType | undefined)[] { | ||
public getTypes(): Readonly<Array<BaseType | undefined>> { | ||
return this.types; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.