TypeScriptで以下の2つのエラーが出てしまっているのですが、これらの原因と解決策は何でしょうか?
エラーメッセージ1はIsEmptyValidatorクラスにvalidateがないってエラーが出ていますが、validateメソッドのことではなくて、validateプロパティーがないって指摘しているんでしょうか?そんなものはなくて当然なのですが。。。
エラーメッセージ2はアブストラクトクラスをインスタンス化してはダメってエラーですが、特にアブストラクトクラス自体をインスタンス化しているつもりもありませんし。。。
エラー1
Type '{ "isEmpty": typeof IsEmptyValidator; }' is not assignable to type '{ [key: string]: Validator; }'.
Property '"isEmpty"' is incompatible with index signature.
Type 'typeof IsEmptyValidator' is not assignable to type 'Validator'.
Property 'validate' is missing in type 'typeof IsEmptyValidator'.
エラー2
Cannot create an instance of the abstract class 'Validator'.
TypeScript
1interface Dictionary<T> { 2 [key: string]: T; 3} 4 5abstract class Validator { 6 7 public abstract validate(); 8 9 constructor() { 10 console.log("super"); 11 } 12 13} 14 15 16class IsEmptyValidator extends Validator { 17 18 public validate() { 19 } 20 21 constructor(){ 22 super(); 23 console.log("isEmpty"); 24 25 } 26 27} 28 29 30// class ValidatorFactory { 31// 32// private validatorMap: Dictionary<Validator> = { 33// 34// "isEmpty": IsEmptyValidator 35// 36// }; 37// 38// constructor() { 39// 40// } 41// 42// public create(validatorType: string) { 43// 44// let validatorToCreate: Validator = new this.validatorMap[validatorType]; 45// 46// return validatorToCreate; 47// 48// } 49// 50// } 51 52 53 54 55class ValidatorFactory { 56 57 //エラー1 58 private validatorMap: {[key: string]: Validator} = { 59 60 "isEmpty": IsEmptyValidator 61 62 }; 63 64 constructor() { 65 66 } 67 68 public create(validatorType: string) { 69 70 //エラー2 71 let validatorToCreate: Validator = new this.validatorMap[validatorType]; 72 73 return validatorToCreate; 74 75 } 76 77}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。