Skip to content

Commit 0ae3841

Browse files
committed
Add supports for index type query and indexed access type
1 parent f55b14f commit 0ae3841

File tree

5 files changed

+76
-3
lines changed

5 files changed

+76
-3
lines changed

samples/keyof.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// copy example from https://github.com/Microsoft/TypeScript/pull/11929
2+
interface Thing {
3+
name: string;
4+
width: number;
5+
height: number;
6+
inStock: boolean;
7+
}
8+
9+
type K1 = keyof Thing; // "name" | "width" | "height" | "inStock"
10+
type K2 = keyof Thing[]; // "length" | "push" | "pop" | "concat" | ...
11+
type K3 = keyof { [x: string]: Thing }; // string
12+
13+
type P1 = Thing["name"]; // string
14+
type P2 = Thing["width" | "height"]; // number
15+
type P3 = Thing["name" | "inStock"]; // string | boolean
16+
type P4 = string["charAt"]; // (pos: number) => string
17+
type P5 = string[]["push"]; // (...items: string[]) => number
18+
19+
// following line will work after merged https://github.com/sjrd/scala-js-ts-importer/pull/47
20+
// type P6 = string[][0]; // string
21+
22+
// extract example from https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/lodash/index.d.ts
23+
interface LoDashStatic {
24+
at<T>(
25+
object: T | null | undefined,
26+
...props: Array<keyof T>
27+
): Array<T[keyof T]>;
28+
}

samples/keyof.ts.scala

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
import scala.scalajs.js
3+
import js.annotation._
4+
import js.|
5+
6+
package keyof {
7+
8+
@js.native
9+
trait Thing extends js.Object {
10+
var name: String = js.native
11+
var width: Double = js.native
12+
var height: Double = js.native
13+
var inStock: Boolean = js.native
14+
}
15+
16+
@js.native
17+
trait LoDashStatic extends js.Object {
18+
def at[T](`object`: T | Null | Unit, props: String*): js.Array[js.Any] = js.native
19+
}
20+
21+
@js.native
22+
@JSGlobalScope
23+
object Keyof extends js.Object {
24+
type K1 = String
25+
type K2 = String
26+
type K3 = String
27+
type P1 = js.Any
28+
type P2 = js.Any
29+
type P3 = js.Any
30+
type P4 = js.Any
31+
type P5 = js.Any
32+
}
33+
34+
}

src/main/scala/org/scalajs/tools/tsimporter/Importer.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,9 @@ class Importer(val output: java.io.PrintWriter) {
330330
case RepeatedType(underlying) =>
331331
TypeRef(Name.REPEATED, List(typeToScala(underlying)))
332332

333+
case IndexedQueryType(_) =>
334+
TypeRef.String
335+
333336
case PolymorphicThisType =>
334337
TypeRef.This
335338

src/main/scala/org/scalajs/tools/tsimporter/Trees.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,9 @@ object Trees {
159159

160160
case class RepeatedType(underlying: TypeTree) extends TypeTree
161161

162+
case class IndexedQueryType(underlying: TypeTree) extends TypeTree
163+
case class IndexedAccessType(objectType: TypeTree, name: TypeTree) extends TypeTree
164+
162165
object PolymorphicThisType extends TypeTree
163166

164167
// Type members

src/main/scala/org/scalajs/tools/tsimporter/parser/TSDefParser.scala

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class TSDefParser extends StdTokenParsers with ImplicitConversions {
4040
"public", "static", "yield",
4141

4242
// Additional keywords of TypeScript
43-
"declare", "module", "type", "namespace"
43+
"declare", "module", "type", "namespace", "keyof"
4444
)
4545

4646
lexical.delimiters ++= List(
@@ -203,10 +203,11 @@ class TSDefParser extends StdTokenParsers with ImplicitConversions {
203203
}
204204

205205
lazy val singleTypeDesc: Parser[TypeTree] =
206-
baseTypeDesc ~ rep("[" ~ "]") ^^ {
206+
baseTypeDesc ~ rep("[" ~> opt(typeDesc) <~ "]") ^^ {
207207
case base ~ arrayDims =>
208208
(base /: arrayDims) {
209-
(elem, _) => ArrayType(elem)
209+
case (elem, None) => ArrayType(elem)
210+
case (elem, Some(index)) => IndexedAccessType(elem, index)
210211
}
211212
}
212213

@@ -218,6 +219,7 @@ class TSDefParser extends StdTokenParsers with ImplicitConversions {
218219
| typeQuery
219220
| tupleType
220221
| thisType
222+
| indexTypeQuery
221223
| "(" ~> typeDesc <~ ")"
222224
)
223225

@@ -248,6 +250,9 @@ class TSDefParser extends StdTokenParsers with ImplicitConversions {
248250
lazy val thisType: Parser[TypeTree] =
249251
"this" ^^^ PolymorphicThisType
250252

253+
lazy val indexTypeQuery: Parser[TypeTree] =
254+
"keyof" ~> typeDesc ^^ IndexedQueryType
255+
251256
lazy val typeQuery: Parser[TypeTree] =
252257
"typeof" ~> rep1sep(ident, ".") ^^ { parts =>
253258
TypeQuery(QualifiedIdent(parts.init.map(Ident), Ident(parts.last)))

0 commit comments

Comments
 (0)