Skip to content
This repository was archived by the owner on Nov 6, 2019. It is now read-only.

Commit 5d48c0e

Browse files
committed
Fix compile errors with generic optional methods
Fixes https://github.com/Kotlin/ts2kt/issues/110
1 parent 6d5ee9a commit 5d48c0e

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-3
lines changed

src/main/kotlin/ts2kt/TypeScriptToKotlinBase.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package ts2kt
22

3+
import converter.replaceTypeParameters
34
import ts2kt.kotlin.ast.*
45
import typescriptServices.ts.ImportEqualsDeclaration
56
import typescriptServices.ts.Symbol
@@ -14,8 +15,13 @@ abstract class TypeScriptToKotlinBase(
1415
open val defaultAnnotations: List<KtAnnotation> = listOf()
1516

1617
open fun addVariable(symbol: Symbol?, name: String, type: KtType, extendsType: KtType? = null, typeParams: List<KtTypeParam>? = null, isVar: Boolean = true, needsNoImpl: Boolean = true, additionalAnnotations: List<KtAnnotation> = listOf(), isOverride: Boolean = false) {
18+
val (typeParamsToKeep, typeParamsToReplace) = (typeParams ?: emptyList()).partition {
19+
typeParam -> extendsType?.typeArgs?.any { it.qualifiedName.name == typeParam.name } ?: false
20+
}
21+
val substitution = typeParamsToReplace.map { it.name.value to (it.upperBound ?: KtType(ANY)).copy(comment = it.name.value) }.toMap()
22+
1723
val annotations = defaultAnnotations + additionalAnnotations
18-
addDeclaration(symbol, KtVariable(KtName(name), KtTypeAnnotation(type), extendsType?.let { KtHeritageType(it) }, annotations, typeParams, isVar = isVar, needsNoImpl = needsNoImpl, isInInterface = isInterface, isOverride = isOverride, hasOpenModifier = hasMembersOpenModifier))
24+
addDeclaration(symbol, KtVariable(KtName(name), KtTypeAnnotation(type.replaceTypeParameters(substitution)), extendsType?.let { KtHeritageType(it) }, annotations, typeParamsToKeep, isVar = isVar, needsNoImpl = needsNoImpl, isInInterface = isInterface, isOverride = isOverride, hasOpenModifier = hasMembersOpenModifier))
1925
}
2026

2127
open fun addFunction(symbol: Symbol?, name: String, callSignature: KtCallSignature, extendsType: KtType? = null, needsNoImpl: Boolean = true, additionalAnnotations: List<KtAnnotation> = listOf(), isOverride: Boolean = false, isOperator: Boolean = false) {

testData/interface/generics/genericOptionalMethods.d.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package genericOptionalMethods
22

33
external interface Foo<T> {
44
val methodWithOutArgs: (() -> Unit)? get() = definedExternally
5-
val <A> methodWithString: ((s: A) -> T)? get() = definedExternally
6-
val <A : T, B> methodWithManyArgs: ((n: A, settings: Bar) -> B)? get() = definedExternally
5+
val methodWithString: ((s: Any /* A */) -> T)? get() = definedExternally
6+
val methodWithManyArgs: ((n: T /* A */, settings: Bar) -> Any /* B */)? get() = definedExternally
7+
val methodWithInOutArg: ((n: T /* A */) -> T /* A */)? get() = definedExternally
78
}

testData/interface/generics/genericOptionalMethods.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ interface Foo<T> {
22
methodWithOutArgs?();
33
methodWithString?<A>(s: A): T;
44
methodWithManyArgs?<A extends T, B>(n: A, settings: Bar): B;
5+
methodWithInOutArg?<A extends T>(n: A): A;
56
}

0 commit comments

Comments
 (0)