Today I encountered an error learning TypeScript polymorphism (i.e., inheritance)
Mistakes show
here indicates an error with super inheritance. String arguments cannot be assigned to string arguments. What the hell is this
To solve the error
See String
, think of it, change to String
try it? So he can
Modified polymorphic code
class Animal {
name:String
constructor(name:String) {
this.name = name
}
eat() {}
}
class Dog extends Animal{
constructor(name:String) {
super(name)
}
eat() {
return this.name + 'Eat Bean'
}
}
class Cat extends Animal{
constructor(name:String) {
super(name)
}
eat() {
return this.name + 'Eat?'
}
}
let dog = new Dog('WW')
console.log(dog.eat())
let cat = new Cat('MM')
console.log(cat.eat())
Questions remain
In fact, the previous use of string
inheritance is shown without error information, I don’t know why there is an error when using polymorphic inheritance, so I can only change it to string
according to the prompt. If you know the reason, please give instruction
Read More:
- [ERR_INVALID_ARG_TYPE]: The “path“ argument must be of type string. Received undefined error
- request.js?b775:101 Uncaught (in promise) Error: Failed to convert value of type ‘java.lang.String’ to required type ‘java.lang.Long’;
- After installing NPM, NRM LS reports an error throw new err_INVALID_ARG_TYPE(name, ‘string‘, value)
- [Vue warn]: Invalid prop: type check failed for prop “index“. Expected String, got Undefined
- [Solved] Invalid prop: type check failed for prop “index“. Expected String, got Undefined
- JS to determine whether the string contains a character
- Typescript error: TSError: x Unable to compile TypeScript…
- JS to find the last character of the string and remove it
- Failed to execute ‘setRequestHeader’ on ‘XMLHttpRequest’: String contains non ISO-8859-1 code point.
- [Vue warn]: Error in callback for watcher “fileList”: “TypeError: Cannot create property ‘uid’ on string
- VScode vue Project Error: Property ‘xxx‘ does not exist on type ‘CombinedVueInstance<{ readyOnly…Vetur(2339)
- node.js yarn Error: SyntaxError: Unexpected string [How to Solve]
- Element-plus Warning: ElementPlusError: [Util] binding value must be a string or number.
- [Solved] Syntax Error: Error: PostCSS received undefined instead of CSS string
- [Solved] Invalid prop: type check failed for prop “modelValue“. Expected Number…
- [Solved] Vuex Error: unknown action type: changeUserInfo
- Syntax Error: Error: PostCSS received undefined instead of CSS string
- [Solved] Warning: To load an ES module, set “type“: “module“ in the package.json or use the .mjs extension
- Error: input is invalid type [How to Solve]
- Using ts-node to Execute .ts files Error [Solved]