Coder Social home page Coder Social logo

Proposal: `PlainObject` about type-fest HOT 5 OPEN

sindresorhus avatar sindresorhus commented on May 22, 2024 3
Proposal: `PlainObject`

from type-fest.

Comments (5)

papb avatar papb commented on May 22, 2024 1

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.

Weakky avatar Weakky commented on May 22, 2024 1

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.

Weakky avatar Weakky commented on May 22, 2024 1

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.

papb avatar papb commented on May 22, 2024 1

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.

papb avatar papb commented on May 22, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.