I want to force all methods return the same type.
Like this:
interface ITextGenerator {
[key: string]: () => string
}
class TextGenerator implements ITextGenerator {
genarator1 = () => "text1"
genarator2 = () => "text2
}
Error in TextGenerator:
Class 'TextGenerator' incorrectly implements interface 'ITextGenerator'. Index signature is missing in type 'TextGenerator'.
You can do the following to make it work:
interface ITextGenerator {
[key: string]: () => string;
}
class TextGenerator implements ITextGenerator{
[key: string]: () => string; // This is important but i think kinda redundant
test = () => '33'; // this is fine
error = 33; // this is not okay
}
I actually don't know the reason why this is needed so if you find out let me know.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.