-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Less restrictive object type #18228
Comments
Isn't this proposed type equivalent to |
Looks like this should be covered by #10715 |
I think the main difference between this and the proposed |
I do not think #10715 is about Asking for unchecked property access on |
I'm not proposing adding properties by assertion, I'm rather proposing a type starting as var o: object;
o.anything.is.possible.without.assertion;
var n: number;
n = o; // but not this, because `o` is a non-primitive object. This basically makes |
so why not |
I want to keep the type safety of the base function foo(bar: object) {
return bar;
}
// Here, you can put anything on the argument if it is a non-primitive object
const f = foo({ arbitrary1: 0, arbitrary2: 1 });
// Now f is an object with arbitrary properties, but suddenly TS warns
f.arbitrary1
//~~~~~~~~~~ Property `arbitrary1` does not exist on type `object`. If I do any: f.arbitrary1 // No problem
1 + f.arbitrary1; // Okay, do it
1 + f; // ?? |
not sure i follow.. var f: object;
f.arbitrary1 // Error: Property 'arbitrary1' does not exisit on type 'object'
1 + f.arbitrary1; // still an Error
1 + f; // Error: Operator '+' can not be applies to '1' and 'object' this seems uniform to me. |
Yes, but that's not a useful behavior. It just acts as an empty interface var f: {};
f.arbitrary1 // Error: Property 'arbitrary1' does not exisit on type 'object'
1 + f.arbitrary1; // still an Error
1 + f; // Error: Operator '+' can not be applies to '1' and 'object' My proposal here is to represent an object type that can have any properties, say a user-defined configuration object bag that is later exposed as a member. BTW, @HerringtonDarkholme, what are the key differences between declare function create(o: {[k: string]: any} | null): void;
create({ prop: 0 }); // OK
create(null); // OK
create(42); // Error
create("string"); // Error
create(false); // Error
create(undefined); // Error |
well, it is safe. if you want the open to all kinda object, use |
how is that different from |
That's my new question, what's the purpose of |
object is useful in an input position to signal something that is not a primitive. it is also useful when you have something you want to treat like a black box. |
If one wants to use Why did TS team decide to introduce a new type when we can use In other words, do these functions have different behaviors?: declare function foo(input: object): void;
declare function foo(input: { [key: string]: any }): void; |
they are close, but they are not the same. |
Still not sure, can you kindly give me an example where |
for a function parameter they should behave the same way. however, a function taking |
Hmm, I see. Thank you for your explanation. |
Seems like we should have a FAQ entry for the differences between |
New behavior
No property existence check for
object
type.Rationale
The current
object
type is useful when used to type function arguments but not very useful on object members. See this IDL:Here, the
details
member can be any "object" depending on payment method behavior. Current lib.d.ts defines this member asany
so that a user can access any untyped members.But
any
means it can beboolean
,number
,string
or evennull
orundefined
.A language service should be able to warn on these cases while still allowing arbitrary member accesses. Allowing such free accesses on
object
type will help here:Hey, a
function
can also be anobject
in TS!Yes, but I don't think the fact will cause any problem as functions can also have arbitrary members. And I'm not proposing arbitrary function call.
The text was updated successfully, but these errors were encountered: