質問編集履歴

1

追記

2022/07/08 17:15

投稿

noc
noc

スコア73

test CHANGED
File without changes
test CHANGED
@@ -14,3 +14,24 @@
14
14
  return set;
15
15
  }
16
16
  ```
17
+
18
+ また、別の例では関数 `bar()` の `isBaz()` までで `target` はプロパティ `baz` を持ち、その型は Baz と確認しているのですが、
19
+ `Property 'baz' does not exist on type 'object'.` というエラーが出ます。
20
+ どう書くのが望ましいでしょうか?
21
+ ```typescript
22
+ type Baz = { [abbr: string]: string };
23
+ function isBaz(target: any): target is Baz {
24
+ return typeof target == "object" &&
25
+ Object.values(target).every((value) => typeof value == "string");
26
+ }
27
+
28
+ function bar(target: unknown) {
29
+ if ( !(target != null && typeof target == "object" && "baz" in target && isBaz(target.baz)) ) {
30
+ throw new Error()
31
+ }
32
+ useBaz(target.baz)
33
+ }
34
+
35
+ function useBaz(baz: Baz) {
36
+ }
37
+ ```