Skip to content

Add supports for index type query and indexed access type #52

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 3, 2017
Merged
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
28 changes: 28 additions & 0 deletions samples/keyof.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// copy example from https://github.com/Microsoft/TypeScript/pull/11929
interface Thing {
name: string;
width: number;
height: number;
inStock: boolean;
}

type K1 = keyof Thing; // "name" | "width" | "height" | "inStock"
type K2 = keyof Thing[]; // "length" | "push" | "pop" | "concat" | ...
type K3 = keyof { [x: string]: Thing }; // string

type P1 = Thing["name"]; // string
type P2 = Thing["width" | "height"]; // number
type P3 = Thing["name" | "inStock"]; // string | boolean
type P4 = string["charAt"]; // (pos: number) => string
type P5 = string[]["push"]; // (...items: string[]) => number

// following line will work after merged https://github.com/sjrd/scala-js-ts-importer/pull/47
// type P6 = string[][0]; // string

// extract example from https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/lodash/index.d.ts
interface LoDashStatic {
at<T>(
object: T | null | undefined,
...props: Array<keyof T>
): Array<T[keyof T]>;
}
34 changes: 34 additions & 0 deletions samples/keyof.ts.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

import scala.scalajs.js
import js.annotation._
import js.|

package keyof {

@js.native
trait Thing extends js.Object {
var name: String = js.native
var width: Double = js.native
var height: Double = js.native
var inStock: Boolean = js.native
}

@js.native
trait LoDashStatic extends js.Object {
def at[T](`object`: T | Null | Unit, props: String*): js.Array[js.Any] = js.native
}

@js.native
@JSGlobalScope
object Keyof extends js.Object {
type K1 = String
type K2 = String
type K3 = String
type P1 = js.Any
type P2 = js.Any
type P3 = js.Any
type P4 = js.Any
type P5 = js.Any
}

}
3 changes: 3 additions & 0 deletions src/main/scala/org/scalajs/tools/tsimporter/Importer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,9 @@ class Importer(val output: java.io.PrintWriter) {
case RepeatedType(underlying) =>
TypeRef(Name.REPEATED, List(typeToScala(underlying)))

case IndexedQueryType(_) =>
TypeRef.String

case PolymorphicThisType =>
TypeRef.This

Expand Down
3 changes: 3 additions & 0 deletions src/main/scala/org/scalajs/tools/tsimporter/Trees.scala
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ object Trees {

case class RepeatedType(underlying: TypeTree) extends TypeTree

case class IndexedQueryType(underlying: TypeTree) extends TypeTree
case class IndexedAccessType(objectType: TypeTree, name: TypeTree) extends TypeTree

object PolymorphicThisType extends TypeTree

// Type members
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class TSDefParser extends StdTokenParsers with ImplicitConversions {
"public", "static", "yield",

// Additional keywords of TypeScript
"declare", "module", "type", "namespace"
"declare", "module", "type", "namespace", "keyof"
)

lexical.delimiters ++= List(
Expand Down Expand Up @@ -203,10 +203,11 @@ class TSDefParser extends StdTokenParsers with ImplicitConversions {
}

lazy val singleTypeDesc: Parser[TypeTree] =
baseTypeDesc ~ rep("[" ~ "]") ^^ {
baseTypeDesc ~ rep("[" ~> opt(typeDesc) <~ "]") ^^ {
case base ~ arrayDims =>
(base /: arrayDims) {
(elem, _) => ArrayType(elem)
case (elem, None) => ArrayType(elem)
case (elem, Some(index)) => IndexedAccessType(elem, index)
}
}

Expand All @@ -218,6 +219,7 @@ class TSDefParser extends StdTokenParsers with ImplicitConversions {
| typeQuery
| tupleType
| thisType
| indexTypeQuery
| "(" ~> typeDesc <~ ")"
)

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

lazy val indexTypeQuery: Parser[TypeTree] =
"keyof" ~> typeDesc ^^ IndexedQueryType

lazy val typeQuery: Parser[TypeTree] =
"typeof" ~> rep1sep(ident, ".") ^^ { parts =>
TypeQuery(QualifiedIdent(parts.init.map(Ident), Ident(parts.last)))
Expand Down