EzDevInfo.com

typescript interview questions

Top typescript frequently asked interview questions

TypeScript with KnockoutJS

Is there any sample of using TypeScript with KnockoutJS? I'm just curious as to how they would work together?

Edit

Here is what I have, seems to work

declare var ko: any;
declare var $: any;
class ViewModel {
    x = ko.observable(10);
    y = ko.observable(10);

}

$(() => {
    ko.applyBindings(new ViewModel());
});

This generates into the following Javascript:

var ViewModel = (function () {
    function ViewModel() {
        this.x = ko.observable(10);
        this.y = ko.observable(10);
    }
    return ViewModel;
})();
$(function () {
    ko.applyBindings(new ViewModel());
});

Source: (StackOverflow)

How do I get jQuery autocompletion in TypeScript?

If I'm working in a TypeScript .ts file, what can I do to get jQuery Intellisense/autocompletion when I type the $ character?

(I'm working in an ASP.NET MVC 3 project in VS 2012.)


Source: (StackOverflow)

Advertisements

What is TypeScript and why would I use it in place of JavaScript?

Can you please describe what the TypeScript language is?

What it can do that JavaScript or available libraries cannot do, that would give me reason to consider it?


Source: (StackOverflow)

How to create enum like type in TypeScript?

I'm working on a definitions file for the Google maps API for TypeScript. But I ran in to a small problem.

I'm trying to define an enum like type eg. google.maps.Animation which contains two properties: BOUNCE and DROP.

How should this be done in TypeScript?


Source: (StackOverflow)

Eclipse plugin for TypeScript? [closed]

The new Microsoft TypeScript language (typed superset of JavaScript) seems very interesting, is there any alpha / incubator project that attempts to support it in Eclipse? Or is it too early to even wish for it


Source: (StackOverflow)

How do I import other TypeScript files?

When using the TypeScript plugin for vs.net, how do I make one TypeScript file import modules declared in other TypeScript files?

file 1:

module moo
{
    export class foo .....
}

file 2:

//what goes here?

class bar extends moo.foo
{
}

Source: (StackOverflow)

get and set in TypeScript

I'm trying to create get and set method for a property:

private _name: string;

Name() {
    get:
    {
        return this._name;
    }
    set:
    {
        this._name = ???;
    }
}

What's the keyword to set a value?


Source: (StackOverflow)

How do I add typescript to an existing Asp.Net MVC project? [duplicate]

This question already has an answer here:

I love the idea behind typescript, but can't seem to figure out how to include it in an ASP.Net MVC project. I've already installed the Visual Studio extension, but I can't seem to find an example or documentation on how to add *.ts files which compile into *.js files.

Edit: Actually, should I replicate the way that the Win8 example .jsproj includes and handles .ts files? Or will that only work for HTML/JS Win8 projects?


Source: (StackOverflow)

TypeScript function overloading

Section 6.3 of the TypeScript language spec talks about function overloading and gives concrete examples on how to implement this. However if I try something like this:

export class LayerFactory { 

    constructor (public styleFactory: Symbology.StyleFactory) { }

    createFeatureLayer (userContext : Model.UserContext, mapWrapperObj : MapWrapperBase) : any {           
         throw "not implemented";
    }                 

    createFeatureLayer(layerName : string, style : any) : any {
        throw "not implemented";
     }        

}

I get a compiler error indicating duplicate identifier even though function parameters are of different types. Even if I add an additional parameter to the second createFeatureLayer function, I still get a compiler error. Ideas, please.


Source: (StackOverflow)

TypeScript private members

I'm looking at implementation of private members in TypeScript, and I find it a little confusing. Intellisense doesn't allow to access private member, but in pure JavaScript, it's all there. This makes me think that TS doesn't implement private members correctly. Any thoughts?

class Test{
  private member: any = "private member";
}
alert(new Test().member);

Source: (StackOverflow)

How Can I Install TypeScript with Visual Studio 2010

This is a popular question that I will provide the answer for.


Source: (StackOverflow)

TypeScript Converting a String to a number

Anyone a suggestion on how to convert a string to a number in TypeScript?

var aNumber : number = "1"; // --> Error

// Could this be done?
var defaultValue = 0;
var aNumber : number = "1".toInt32(defaultValue);

// Or ..
var defaultValue = 0;
var aNumber : number = StringToInt("1", defaultValue);

Update: I did some extra puzzling, the best sofar I've come up with:

var aNumber : number = (<any> "1") * 1;

Source: (StackOverflow)

Debugging TypeScript code with Visual Studio

Is there a way to debug TypeScript source in Visual Studio (instead of debugging the generated javascript)?

From the TypeScript language specifications: "TypeScript optionally provides source maps, enabling source-level debugging."

I was therefore expecting to be able to place breakpoints in ts code and be able to debug it, but it doesn't work. I didn't find any other mentions of debugging in the specs. Is there anything I should do to make this work? Perhaps the word "optionally" hints that I need to do something for it to work... Any suggestions?


Source: (StackOverflow)

Declaring abstract method in TypeScript

I am trying to figure out how to correctly define abstract methods in TypeScript:

Using the original inheritance example:

class Animal {
    constructor(public name) { }
    makeSound(input : string) : string;
    move(meters) {
        alert(this.name + " moved " + meters + "m.");
    }
}

class Snake extends Animal {
    constructor(name) { super(name); }
    makeSound(input : string) : string {
        return "sssss"+input;
    }
    move() {
        alert("Slithering...");
        super.move(5);
    }
}

I would like to know how to correctly define method makeSound, so it is typed and possible to overried.

Also, I am not sure how to define correctly protected methods - it seems to be a keyword, but has no effect and the code won't compile.


Source: (StackOverflow)

How does typescript interfaces with construct signatures work?

I am having some trouble working out how defining constructors in interfaces work. I might be totally misunderstanding something. But I have searched for answers for a good while and I can not find anything related to this.

How do I implement the following interface in a TypeScript class:

interface MyInterface {
    new ( ... ) : MyInterface;
}

Anders Hejlsberg creates an interface containing something similar to this in this video (at around 14 minutes). But for the life of me I can not implement this in a class.

I am probably misunderstanding something, what am I not getting?

EDIT:

To clarify. With "new ( ... )" I meant "anything". My problem is that I can not get even the most basic version of this working:

interface MyInterface {
    new () : MyInterface;
}

class test implements MyInterface {
    constructor () { }
}

This is not compiling for me I get "Class 'test' declares interface 'MyInterface' but does not implement it: Type 'MyInterface' requires a construct signature, but Type 'test' lacks one" when trying to compile it.

EDIT:

So after researching this a bit more given the feedback.

interface MyInterface {
    new () : MyInterface;
}

class test implements MyInterface {
    constructor () => test { return this; }
}

Is not valid TypeScript and this does not solve the problem. You can not define the return type of the constructor. It will return "test". The signature of the following: class test { constructor () { } } Seems to be "new () => test" (obtained by hovering over "class" in the online editor with just that code pasted in). And this is what we would want and what i thought it would be.

Can anyone provide an example of this or something similar where it is actually compiling?

EDIT (again...):

So I might have come up with an idea as to why it is possible to define this in an interface but not possible to implement in a TypeScript class.The following works:

var MyClass = (function () {
    function MyClass() { }
    return MyClass;
})();

interface MyInterface {
    new () : MyInterface;
}

var testFunction = (foo: MyInterface) : void =>  { }
var bar = new MyClass();
testFunction(bar);

So is this only a feature of TypeScript that lets you interface javascript? Or is it possible to implement it in TypeScript without having to implement the class using javascript?


Source: (StackOverflow)