class test<T> {
copyObject<T> (object:T):T {
var objectCopy = <T>{};
for (var key in object) {
if (object.hasOwnProperty(key)) {
objectCopy[key] = object[key];
}
}
return objectCopy;
}
}
Se eseguo il codice come segue
c:\Work\TypeScript>tsc hello.ts
funziona OK. Tuttavia, il codice seguente:
c:\Work\TypeScript>tsc --noImplicitAny hello.ts
getta
hello.ts(6,17): error TS7017: Index signature of object type implicitly has an 'any' type.
hello.ts(6,35): error TS7017: Index signature of object type implicitly has an 'any' type.
Quindi, se si disattiva noImplicitAny
bandiera, funzionerà.
Sembra che ci sia un'altra opzione anche perché tsc
supporta il flag seguente:
--suppressImplicitAnyIndexErrors Suppress noImplicitAny errors for indexing objects lacking index signatures.
Questo funziona anche per me:
tsc --noImplicitAny --suppressImplicitAnyIndexErrors hello.ts
Aggiornamento:
class test<T> {
copyObject<T> (object:T):T {
let objectCopy:any = <T>{};
let objectSource:any = object;
for (var key in objectSource) {
if (objectSource.hasOwnProperty(key)) {
objectCopy[key] = objectSource[key];
}
}
return objectCopy;
}
}
Questo codice funziona senza modificare alcuna compilation er bandiere.
fonte
2015-12-29 11:22:06
Penso che sia a causa della 'var key in object' e dell'uso di' key' in seguito. Puoi provare a eseguire la compilation con 'noImplicitAny = false' per confermare? – FlorianTopf