Made additionalProperties add an index signature to unkown when explicit properties are specified#1719
Conversation
…cit properties are specified
|
Hey @filkalny-thimble, why do you prefer unknown instead of any? |
const anyTyped: any = { foo: "bar" }
type MyType = { foo: { faz: string } }
const concreteTyped: MyType = anyTyped; // TS sees no issues with this
// This will fail with "cannot access properly length of undefined"
console.log(concreteTyped.foo.faz.length);The same example with const unknownTyped: unknown = { foo: "bar" }
type MyType = { foo: { faz: string } }
// TS would not be happy with the following:
// const concreteTyped: MyType = unknownTyped;
// so instead you have to narrow the type
const isMyType = (val: unknown): val is MyType => typeof val === "object" && "foo" in val && typeof val.foo === "object" && "faz" in val.foo && typeof val.foo.faz === "string";
if (isMyType(unknownTyped)) {
const concreteTyped: MyType = unknownTyped;
// Now we can operate safely
console.log(concreteTyped.foo.faz.length);
} |
|
@filkalny-thimble I see. OpenAPI has a concept of Any Type schema that matches any data type. Would you also expect Any Type schema to result in |
Yes, this is what I would expect, despite the coincidentally similar naming to typescript's |
|
@filkalny-thimble Great, I was hoping that would be the answer. I think we have two approaches towards handling these cases, |
The only reasonable case I would see for having a user switch built in is so that a breaking change is not introduced into existing code bases. This option should be communicated as such; maybe |
|
@filkalny-thimble I see. I might make the switch to unknown as it makes sense. We've got additionalProperties working in our fork, just need to offer the option to make them unknown over any |
|
@filkalny-thimble hej, I released v0.27.30 with a bunch of |
This allows objects with additional properties to be used without polluting the rest of the codebase with
anys.