回答編集履歴

1

ソースコードにコメント追記

2021/10/21 15:29

投稿

UnripeTomato
UnripeTomato

スコア25

test CHANGED
@@ -62,6 +62,8 @@
62
62
 
63
63
 
64
64
 
65
+ //ここで型ガード関数作成
66
+
65
67
  const isString = (value: unknown): value is string =>
66
68
 
67
69
  typeof value === "string";
@@ -71,6 +73,8 @@
71
73
  if (isString(objInstance[key as keyof typeof objInstance])) {
72
74
 
73
75
  if (isString(forInput[key as keyof typeof forInput])) {
76
+
77
+ //代入する時も as string にキャストしてから入れる
74
78
 
75
79
  (objInstance[key as keyof typeof objInstance] as string) = forInput[
76
80
 
@@ -88,7 +92,9 @@
88
92
 
89
93
  console.log(objInstance);
90
94
 
95
+ 期待する結果となった
96
+
91
- baseClass { id: 123, name: 'base1', name2: 'input name' }
97
+ //baseClass { id: 123, name: 'base1', name2: 'input name' }
92
98
 
93
99
  ```
94
100