Comments (5)
Your suggestion does not prevent nested things. Also what is the issue with arrays? I don't see any issue for a POJO to have an array, is there any?
I think the JsonObject
provided by type-fest is probably what you want.
from type-fest.
I don't see any issue for a POJO to have an array, is there any?
The point here is not to prevent a POJO to have an array, the point is to prevent a POJO to be an array.
I think the JsonObject provided by type-fest is probably what you want.
JsonObject
is not what I want as it doesn't allow classes and functions as values.
const obj: JsonObject = {
a: () => {} // type error
}
Put differently, the PlainObject
type achieves the same as what lodash.isPlainObject does, but at the type level.
_.isPlainObject(new Foo); // => false
const obj: PlainObject = new Foo // type error
_.isPlainObject([1, 2, 3]); // => false
const obj: PlainObject = [1, 2, 3] // type error
_.isPlainObject({ 'x': 0, 'y': 0 }); // => true
const obj: PlainObject = { 'x': 0, 'y': 0 } // working
_.isPlainObject(Object.create(null)); // => true
const obj: PlainObject = Object.create(null) // working
from type-fest.
Oh yeah, I re-read the issue and saw how it wasn't really clear given my wording. Sorry about that too 🙏
This is so surprising to me that I would actually think this is a bug in TypeScript. What is the explanation for this?
My theory is that { [props: string]: any }
gets "transformed" into an object
type, which allows both arrays and functions.
I didn't open a TS issue, but I found no question at all about this while googling so I ended up thinking it was more or less assumed correct by everyone 🙈
Is any the only type that does not give an error? Or is there another Foo that also does not give an error?
any
is the only one I found too.
This was found by an actual bug in one of our open-source project. The code looked like this, see if you can spot the problem 🤦♂️:
function pluginHook(): { [props: string]: any } {
const internalHook = () => {
return {
some: { value: true }
}
}
return internalHook
}
from type-fest.
Interesting, perhaps someone else can shed some light in this topic.
see if you can spot the problem
Forgot to call the function I guess 😅
from type-fest.
Oh, wow, I get it now!! The fact that { [props: string]: any }
accepts () => {}
itself was so unexpected to me that I didn't even parse correctly what you were saying. Sorry about that.
// This is not an error! Wow!!
const foo: { [props: string]: any } = () => {}
This is so surprising to me that I would actually think this is a bug in TypeScript. What is the explanation for this?
Out of curiosity, I tried the following:
type Primitive = string | number | boolean | null | undefined | symbol | bigint
type Foo<T> = { [x: string]: T }
const foo1: Foo<any> = () => { } // OK (????????)
const foo2: Foo<unknown> = () => { } // Error
const foo3: Foo<never> = () => { } // Error
const foo4: Foo<Primitive> = () => { } // Error
const foo5: Foo<() => void> = () => { } // Error
Is any
the only type that does not give an error? Or is there another Foo<MagicalTypeHere>
that also does not give an error?
from type-fest.
Related Issues (20)
- How can I use the classic Omit functionality applied to an array type instead of a type? HOT 2
- PartialOnUndefinedDeep removes non-optional properties when the input type contains an index signature HOT 1
- [4.8.0] fails when there are no DOM types HOT 1
- [4.8.0] type error on jsonify HOT 5
- Audit all types for whether they need `readonly` modifier HOT 1
- "Stricter" intersection type? HOT 2
- `ConditionalSimplifyDeep` is not exported from root HOT 1
- Proposal: `PickNonNever<Type>`
- v4.8.0 break SnakeCasedPropertiesDeep with enum HOT 1
- "Writable" is imported from external module "./writable.js" but never used in
- `Paths` should not recurse into `Map`'s prototype properties. HOT 9
- ConditionalPickDeep does not work with deep interfaces
- `Addition` And `Subtraction` Type Proposal HOT 3
- `MergeDeep` removes optionals from `any` and `never` fields HOT 2
- Nested structure without index signature is not assignable to `Jsonifiable` HOT 1
- MergeDeep is never when compiled to .d.ts HOT 5
- ReadonlyDeep turns `void` into `unknown`
- Canary: `MergeDeep` failures in `typescript@next` not detected by canary tests HOT 3
- 4.9.0 fails with "Cannot find module './camel-case' or its corresponding type declarations" HOT 1
- IfType type guards HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from type-fest.