TypeScript–noImplicitAny option, the Other

I did not know for a long time,
Any implicit error
It was a reference to the property that has not been defined

//Index Signature

var ok1:{
[index : string]:string;} = {};

var ok2:{
[index:string]:any;
name :string;
age: string;
}
var ok2Same: {
name : string;
age: number;
};
var v1 = ok2Same[“noExists”];
//Non-existent parameters become any implicit
//if you use –noImplicitAny option, this is error
var v2 = ok2Same[“name”];
var v3 = ok2Same[“age”];

//Constructor Signature

var Greeting:{
new(word:string): {hello():string;};
}
class GreetingImpl{
constructor(public word = “word”){}
hello(){
return “Hello” + this.word;
}
}
Greeting = GreetingImpl;
var obj = new Greeting(“word”);
var str = obj.hello();
console.log(str);

//Generics

//if typeparameter same, independented function
function head(array: T[]):T {
return array[0];
}
function last(array:T[]):T{
return array[array.length-1];
}

var array = [1,2,3];
console.log(head(array));//1
console.log(last(array));//3

(Visited 1 times, 1 visits today)

コメントを残す

メールアドレスが公開されることはありません。