Skip to content

Commit

Permalink
Basic support for conditional types
Browse files Browse the repository at this point in the history
  • Loading branch information
sgrishchenko committed Dec 18, 2023
1 parent 2439139 commit 28570f3
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/converter/plugins/convertConditionalType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import ts, {ConditionalTypeNode} from "typescript";
import {createSimplePlugin} from "../plugin.js";
import {TypeScriptService, typeScriptServiceKey} from "./TypeScriptPlugin.js";
import {isPossiblyNullableType} from "./NullableUnionTypePlugin.js";

export const convertConditionalType = createSimplePlugin((node, context, render) => {
if (!ts.isConditionalTypeNode(node)) return null

const typeScriptService = context.lookupService<TypeScriptService>(typeScriptServiceKey)

const isNullableTrue = isPossiblyNullableType(node.trueType, context)
const isNullableFalse = isPossiblyNullableType(node.falseType, context)
const isNullable = isNullableTrue || isNullableFalse

return `Any${isNullable ? "?" : ""} /* ${typeScriptService?.printNode(node)} */`;
})
2 changes: 2 additions & 0 deletions src/defaultPlugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ import {convertLiteral} from "./converter/plugins/convertLiteral.js";
import {inheritedTypeLiteralPlugin} from "./converter/plugins/InheritedTypeLiteralPlugin.js";
import {Injection} from "./converter/injection.js";
import {InjectionPlugin} from "./converter/plugins/InjectionPlugin.js";
import {convertConditionalType} from "./converter/plugins/convertConditionalType.js";

const hasKind = (kind: ts.SyntaxKind) => (node: Node) => node.kind === kind

Expand Down Expand Up @@ -149,4 +150,5 @@ export const createPlugins = (
convertMappedType,
convertIndexedSignatureDeclaration,
convertTypeQuery,
convertConditionalType,
]
14 changes: 14 additions & 0 deletions test/functional/base/generated/conditionalType/simple.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Generated by Karakum - do not modify it manually!

@file:JsModule("sandbox-base/conditionalType/simple")
@file:Suppress(
"NON_EXTERNAL_DECLARATION_IN_INAPPROPRIATE_FILE",
)

package sandbox.base.conditionalType



typealias TestConditional<S> = Any /* S extends () => infer P ? Promise<P> : Promise<void> */

typealias TestNullableConditional<S> = Any? /* S extends () => infer P ? Promise<P> | null : Promise<void> */
3 changes: 3 additions & 0 deletions test/functional/base/lib/conditionalType/simple.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
type TestConditional<S> = S extends () => infer P ? Promise<P> : Promise<void>;

type TestNullableConditional<S> = S extends () => infer P ? Promise<P> | null : Promise<void>;

0 comments on commit 28570f3

Please sign in to comment.