Skip to content
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

Why is there no type t.object? #86

Closed
jez9999 opened this issue Nov 7, 2017 · 10 comments
Closed

Why is there no type t.object? #86

jez9999 opened this issue Nov 7, 2017 · 10 comments

Comments

@jez9999
Copy link

jez9999 commented Nov 7, 2017

When I'm making an interface I'd like to set a property to type t.object so that it ensures it is set to some object. Why does t.object not exist?

@jez9999
Copy link
Author

jez9999 commented Nov 7, 2017

@gcanti Is there a technical reason why t.object isn't there or is it just an oversight?

@phiresky
Copy link
Contributor

phiresky commented Nov 7, 2017

I think the typescript object type did not exist yet when this library was created (introduced in TS 2.2: https://blog.mariusschulz.com/2017/02/24/typescript-2-2-the-object-type)

@jez9999
Copy link
Author

jez9999 commented Nov 7, 2017

Sounds like there's no good reason not to add it now then? Looks like it could pretty much copy/paste the string type and replace string with object.

@gcanti
Copy link
Owner

gcanti commented Nov 7, 2017

Indeed, when I first wrote io-ts it didn't exist.

The difference from { [key: string]: any } (i.e Dictionary) seems accessing by index which is not allowed. You can't even use typeof to refine

declare function f(x: { [key: string]: any }): void

f(1) // error
f('foo') // error
f(true) // error
f(null) // error
f(undefined) // error
f({}) // ok
f([]) // ok

declare function g(x: object): void

g(1) // error
g('foo') // error
g(true) // error
g(null) // error
g(undefined) // error
g({}) // ok
g([]) // ok

declare const x: { [key: string]: any }
declare const y: object

const xx = x['foo'] // ok
const yy = y['foo'] // error: Element implicitly has an 'any' type because type '{}' has no index signature.

const xxx = x.foo // ok
const yyy = y.foo // error: Property 'foo' does not exist on type 'object'

if (typeof y['foo'] !== 'undefined') { // error

}

@jez9999
Copy link
Author

jez9999 commented Nov 7, 2017

Can you add it?

gcanti added a commit that referenced this issue Nov 8, 2017
@gcanti gcanti closed this as completed in 8fa3ccb Nov 8, 2017
@gcanti
Copy link
Owner

gcanti commented Nov 8, 2017

@jez9999 released

@jez9999
Copy link
Author

jez9999 commented Nov 8, 2017

Great!

@jez9999
Copy link
Author

jez9999 commented Nov 8, 2017

@gcanti Should a t.interface type be able to be used where a t.object is required? TypeScript says the types are incompatible but since an interface is always implemented by an object, shouldn't it count as a t.object?

@phiresky
Copy link
Contributor

phiresky commented Nov 9, 2017 via email

@jez9999
Copy link
Author

jez9999 commented Nov 9, 2017

Well is there a way for me to say "an interface fulfilled by an object" then? That's the kind of semantics I want here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants