回答編集履歴
1
ソースコードにコメント追記
answer
CHANGED
@@ -30,11 +30,13 @@
|
|
30
30
|
typeof forInput[key as keyof typeof forInput] === "string"
|
31
31
|
) {
|
32
32
|
|
33
|
+
//ここで型ガード関数作成
|
33
34
|
const isString = (value: unknown): value is string =>
|
34
35
|
typeof value === "string";
|
35
36
|
|
36
37
|
if (isString(objInstance[key as keyof typeof objInstance])) {
|
37
38
|
if (isString(forInput[key as keyof typeof forInput])) {
|
39
|
+
//代入する時も as string にキャストしてから入れる
|
38
40
|
(objInstance[key as keyof typeof objInstance] as string) = forInput[
|
39
41
|
key as keyof typeof forInput
|
40
42
|
] as string;
|
@@ -43,7 +45,8 @@
|
|
43
45
|
}
|
44
46
|
});
|
45
47
|
console.log(objInstance);
|
48
|
+
期待する結果となった
|
46
|
-
baseClass { id: 123, name: 'base1', name2: 'input name' }
|
49
|
+
//baseClass { id: 123, name: 'base1', name2: 'input name' }
|
47
50
|
```
|
48
51
|
|
49
52
|
tsc後のjs抜粋
|