Skip to content
This repository was archived by the owner on Nov 6, 2019. It is now read-only.
Open
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
8 changes: 7 additions & 1 deletion src/main/kotlin/ts2kt/TypeScriptToKotlinBase.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ts2kt

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

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) {
val (typeParamsToKeep, typeParamsToReplace) = (typeParams ?: emptyList()).partition {
typeParam -> extendsType?.typeArgs?.any { it.qualifiedName.name == typeParam.name } ?: false
}
val substitution = typeParamsToReplace.map { it.name.value to (it.upperBound ?: KtType(ANY)).copy(comment = it.name.value) }.toMap()

val annotations = defaultAnnotations + additionalAnnotations
addDeclaration(symbol, KtVariable(KtName(name), KtTypeAnnotation(type), extendsType?.let { KtHeritageType(it) }, annotations, typeParams, isVar = isVar, needsNoImpl = needsNoImpl, isInInterface = isInterface, isOverride = isOverride, hasOpenModifier = hasMembersOpenModifier))
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))
}

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) {
Expand Down
5 changes: 3 additions & 2 deletions testData/interface/generics/genericOptionalMethods.d.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package genericOptionalMethods

external interface Foo<T> {
val methodWithOutArgs: (() -> Unit)? get() = definedExternally
val <A> methodWithString: ((s: A) -> T)? get() = definedExternally
val <A : T, B> methodWithManyArgs: ((n: A, settings: Bar) -> B)? get() = definedExternally
val methodWithString: ((s: Any /* A */) -> T)? get() = definedExternally
val methodWithManyArgs: ((n: T /* A */, settings: Bar) -> Any /* B */)? get() = definedExternally
val methodWithInOutArg: ((n: T /* A */) -> T /* A */)? get() = definedExternally
}
1 change: 1 addition & 0 deletions testData/interface/generics/genericOptionalMethods.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ interface Foo<T> {
methodWithOutArgs?();
methodWithString?<A>(s: A): T;
methodWithManyArgs?<A extends T, B>(n: A, settings: Bar): B;
methodWithInOutArg?<A extends T>(n: A): A;
}