1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
|
tests/cases/conformance/types/thisType/thisTypeInFunctions2.ts(14,5): error TS7010: 'foo', which lacks return-type annotation, implicitly has an 'any' return type.
==== tests/cases/conformance/types/thisType/thisTypeInFunctions2.ts (1 errors) ====
interface IndexedWithThis {
// this is a workaround for React
init?: (this: this) => void;
willDestroy?: (this: any) => void;
[propName: string]: number | string | boolean | symbol | undefined | null | {} | ((this: any, ...args:any[]) => any);
}
interface IndexedWithoutThis {
// this is what React would like to write (and what they write today)
init?: () => void;
willDestroy?: () => void;
[propName: string]: any;
}
interface SimpleInterface {
foo(n: string);
~~~~~~~~~~~~~~~
!!! error TS7010: 'foo', which lacks return-type annotation, implicitly has an 'any' return type.
bar(): number;
}
declare function extend1(args: IndexedWithThis): void;
declare function extend2(args: IndexedWithoutThis): void;
declare function simple(arg: SimpleInterface): void;
extend1({
init() {
this // this: IndexedWithThis because of contextual typing.
// this.mine
this.willDestroy
},
mine: 12,
foo() {
this.url; // this: any because 'foo' matches the string indexer
this.willDestroy;
}
});
extend2({
init() {
this // this: IndexedWithoutThis because of contextual typing
this.mine
},
mine: 13,
foo() {
this // this: IndexedWithoutThis because of contextual typing
this.mine
}
});
simple({
foo(n) {
return n.length + this.bar();
},
bar() {
return 14;
}
})
|