-
Notifications
You must be signed in to change notification settings - Fork 205
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add build and test task * Implement crude conditional type node parser * Fix linter warning * Add some tests for conditional type * Only unwrap types internally when needed * Update schema URLs to draft-07 * Use already existing derefType * Remove accidentally commited tasks.json * Split types "any" and "unknown" This is needed because for isAssignableTo checks these types works differently * Add NeverTypeFormatter in case this type is exposed * Add new classes to index * Move isAssignable check to utility function * Better variable names * Fix stack overflow in isAssignableTo for circular dependencies * Simplify condition type node parser * Add test for Omit * Simplified conditional type node parser even more * Support enums in conditionals types * Combine unions * Deref type * More tests * Improve type narrowing so definition types are kept * Remove accidentally commited comment * Only narrow down result type when type parameter matching the check type * Ignore annotations from standard typescript types
- Loading branch information
Showing
40 changed files
with
1,424 additions
and
7 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
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,50 @@ | ||
import * as ts from "typescript"; | ||
import { Context, NodeParser } from "../NodeParser"; | ||
import { SubNodeParser } from "../SubNodeParser"; | ||
import { BaseType } from "../Type/BaseType"; | ||
import { isAssignableTo } from "../Utils/isAssignableTo"; | ||
import { narrowType } from "../Utils/narrowType"; | ||
|
||
export class ConditionalTypeNodeParser implements SubNodeParser { | ||
public constructor( | ||
private typeChecker: ts.TypeChecker, | ||
private childNodeParser: NodeParser, | ||
) {} | ||
|
||
public supportsNode(node: ts.ConditionalTypeNode): boolean { | ||
return node.kind === ts.SyntaxKind.ConditionalType; | ||
} | ||
|
||
public createType(node: ts.ConditionalTypeNode, context: Context): BaseType { | ||
const checkType = this.childNodeParser.createType(node.checkType, context); | ||
const extendsType = this.childNodeParser.createType(node.extendsType, context); | ||
const result = isAssignableTo(extendsType, checkType); | ||
const tsResultType = result ? node.trueType : node.falseType; | ||
const resultType = this.childNodeParser.createType(tsResultType, context); | ||
|
||
// If result type is the same type parameter as the check type then narrow down the result type | ||
const checkTypeParameterName = this.getTypeParameterName(node.checkType); | ||
const resultTypeParameterName = this.getTypeParameterName(tsResultType); | ||
if (resultTypeParameterName != null && resultTypeParameterName === checkTypeParameterName) { | ||
return narrowType(resultType, type => isAssignableTo(extendsType, type) === result); | ||
} | ||
|
||
return resultType; | ||
} | ||
|
||
/** | ||
* Returns the type parameter name of the given type node if any. | ||
* | ||
* @param node - The type node for which to return the type parameter name. | ||
* @return The type parameter name or null if specified type node is not a type parameter. | ||
*/ | ||
private getTypeParameterName(node: ts.TypeNode): string | null { | ||
if (ts.isTypeReferenceNode(node)) { | ||
const typeSymbol = this.typeChecker.getSymbolAtLocation(node.typeName)!; | ||
if (typeSymbol.flags & ts.SymbolFlags.TypeParameter) { | ||
return typeSymbol.name; | ||
} | ||
} | ||
return null; | ||
} | ||
} |
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,14 @@ | ||
import * as ts from "typescript"; | ||
import { Context } from "../NodeParser"; | ||
import { SubNodeParser } from "../SubNodeParser"; | ||
import { BaseType } from "../Type/BaseType"; | ||
import { NeverType } from "../Type/NeverType"; | ||
|
||
export class NeverTypeNodeParser implements SubNodeParser { | ||
public supportsNode(node: ts.KeywordTypeNode): boolean { | ||
return node.kind === ts.SyntaxKind.NeverKeyword; | ||
} | ||
public createType(node: ts.KeywordTypeNode, context: Context): BaseType { | ||
return new NeverType(); | ||
} | ||
} |
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,14 @@ | ||
import * as ts from "typescript"; | ||
import { Context } from "../NodeParser"; | ||
import { SubNodeParser } from "../SubNodeParser"; | ||
import { BaseType } from "../Type/BaseType"; | ||
import { UnknownType } from "../Type/UnknownType"; | ||
|
||
export class UnknownTypeNodeParser implements SubNodeParser { | ||
public supportsNode(node: ts.KeywordTypeNode): boolean { | ||
return node.kind === ts.SyntaxKind.UnknownKeyword; | ||
} | ||
public createType(node: ts.KeywordTypeNode, context: Context): BaseType { | ||
return new UnknownType(); | ||
} | ||
} |
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,7 @@ | ||
import { BaseType } from "./BaseType"; | ||
|
||
export class NeverType extends BaseType { | ||
public getId(): string { | ||
return "never"; | ||
} | ||
} |
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,7 @@ | ||
import { BaseType } from "./BaseType"; | ||
|
||
export class UnknownType extends BaseType { | ||
public getId(): string { | ||
return "unknown"; | ||
} | ||
} |
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,16 @@ | ||
import { Definition } from "../Schema/Definition"; | ||
import { SubTypeFormatter } from "../SubTypeFormatter"; | ||
import { BaseType } from "../Type/BaseType"; | ||
import { NeverType } from "../Type/NeverType"; | ||
|
||
export class NeverTypeFormatter implements SubTypeFormatter { | ||
public supportsType(type: NeverType): boolean { | ||
return type instanceof NeverType; | ||
} | ||
public getDefinition(type: NeverType): Definition { | ||
return {not: {}}; | ||
} | ||
public getChildren(type: NeverType): BaseType[] { | ||
return []; | ||
} | ||
} |
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,16 @@ | ||
import { Definition } from "../Schema/Definition"; | ||
import { SubTypeFormatter } from "../SubTypeFormatter"; | ||
import { BaseType } from "../Type/BaseType"; | ||
import { UnknownType } from "../Type/UnknownType"; | ||
|
||
export class UnknownTypeFormatter implements SubTypeFormatter { | ||
public supportsType(type: UnknownType): boolean { | ||
return type instanceof UnknownType; | ||
} | ||
public getDefinition(type: UnknownType): Definition { | ||
return {}; | ||
} | ||
public getChildren(type: UnknownType): BaseType[] { | ||
return []; | ||
} | ||
} |
Oops, something went wrong.