8000 Change 'extra properties' type from `any` to `unknown` · abhijit-hota/typescript-book@17b5cac · GitHub
[go: up one dir, main page]

Skip to content

Commit 17b5cac

Browse files
authored
Change 'extra properties' type from any to unknown
I think this is a much better practice, as it will prevent the developer from doing something like: ``` function logIfHasName(something: { name?: string, [key: string]: unknown}) { if (something.name) { console.log(something.name); console.log(something.foo.bar); //Object is of type 'unknown'.(2571) } } function logIfHasName2(something: { name?: string, [key: string]: any}) { if (something.name) { console.log(something.name); console.log(something.foo.bar); //is allowed } } ``` https://www.typescriptlang.org/play?#code/GYVwdgxgLglg9mABAGzgcwJLABIEMDOAcrgLYCmAFPnOVABYxhoBciA3omKWQPyv5QATozQAaRAG0A1mQCe-ISIC6rcFLBwA7mAC+ASnYAoRCcQxgiKjTL0RAOi7kDbY6bcQE1ZGTuo0V2gYmB249AG5XNxMPMC8fPwCbILQ7YDg4OwAjXEFwxAB6fIB5TIArMmgzfEQ4CyhZAAcyRAByNQ1tFrsKACYAVgB2AEY9SMQdQwnDUEhYBBR0LDwibh7E2yZWDkdeBWEmcWk5PeVWXDBZfSM3c0tqQPsd5zH3TzhvX3R15JCnCKjTDE4p9-PckvY0hlsrkwgV8jBqrhkKhNGQACZjCY6IA
1 parent 3ecf48a commit 17b5cac

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

docs/types/freshness.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ The reason why only object literals are type checked this way is because in this
5959
A type can include an index signature to explicitly indicate that excess properties are permitted:
6060

6161
```ts
62-
var x: { foo: number, [x: string]: any };
62+
var x: { foo: number, [x: string]: unknown };
6363
x = { foo: 1, baz: 2 }; // Ok, `baz` matched by index signature
6464
```
6565

0 commit comments

Comments
 (0)
0