1

Just for fun, I decided to look at TypeScript.

I have one question (so far); given the following typescript code...

class EP {

  id: number;
  TY: string;

  constructor(id: number, ty: string) {
    this.id = id;
    this.TY = ty;
  }

}

and the following typescript code...

var ep = new EP(1, "Yes");
var x = [ep.id, ep.TY];

JavaScript would see this as valid, however TypeScript is complaining of "Incompatible types in array literal expression". How should I annotate "x" in order to clear this error?

For those who are interested, I need to create the array to pass values to SQLite.

Additionally, what would be the syntax for the same when using this as a parameter to a function call, such as an SQLite insert...

db.transaction.executeSql("...", [ep.id, ep.TY], ...);

Thanks.

1 Answer 1

7

Try any[] for your array's type.

Sign up to request clarification or add additional context in comments.

9 Comments

Can you elaborate; I thought it was something like that but cannot seem to find the syntax. any[ep.id, ep.TY] is apparently incorrect according to the playground.
var x:any[] = [ep.id, ep.TY];
db.transaction.executeSql("...", <any[]>[ep.id, ep.TY], ...);
I'd also note that have to add this annotation shouldn't be required going forward (and arguably never should have been). The compiler should infer a "best common type" for expressions where multiple types need to be taken into account (e.g. functions with multiple return statements, or array literals). See section 3.10 in the latest spec updates (typescript.codeplex.com/SourceControl/changeset/… Language Specification.docx )
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.