Coder Social home page Coder Social logo

Comments (4)

Chnapy avatar Chnapy commented on June 27, 2024 1

Fix published, please upgrade to 1.3.0 👍

from tiled-types.

Chnapy avatar Chnapy commented on June 27, 2024 1

I don't think it's possible, because of how array filter works.
Let me show you a basic example:

type Sample = { type: 'int'; a: 1 } | { type: 'foo'; b: 7 };

const sample: Sample = null as any;

if(sample.type === 'int') {
    sample.a;    // works
}

const list: Sample[] = [sample];

list
    .filter((p) => p.type === "int")
    .map(p => p.a)  // error, because at this stage we don't know p.type value

Unlike the if statement, array filter do not "keep" the type inference.
The proper way I know in your case is doing like that:

const { shadowGid, shadowX, shadowY, tintcolor, opacity } = mergeAll(
      properties
        .filter((p): p is TiledPropertyAbstract<number, 'int'> => p.type === "int")
        .map((p) => ({ [p.name]: p.value }))
    );

It's still a cast tho.

from tiled-types.

Chnapy avatar Chnapy commented on June 27, 2024

Hi,
Thank you for your feedback, I'll try to do required changes very soon.

For the record, after quick investigation I got this result:

These properties
image
give these JSON props

[
        {
         "name":"azerty",
         "type":"color",
         "value":"#ffff5500"
        }, 
        {
         "name":"foo",
         "type":"string",
         "value":"bar"
        }, 
        {
         "name":"tata",
         "type":"float",
         "value":8.89
        }, 
        {
         "name":"titi",
         "type":"file",
         "value":"Links.txt"
        }, 
        {
         "name":"toto",
         "type":"bool",
         "value":true
        }, 
        {
         "name":"tutu",
         "type":"int",
         "value":8
        }, 
        {
         "name":"tyty",
         "type":"object",
         "value":7
        }
]

from tiled-types.

AdrienLemaire avatar AdrienLemaire commented on June 27, 2024

@Chnapy thank you for that!

I have another question in regards to this type.

Given the code:

    const { shadowGid, shadowX, shadowY, tintcolor, opacity } = mergeAll(
      properties
        .filter((p) => p.type === "int")
        .map((p) => ({ [p.name]: p.value }))
    );

I'd like all my destructured variables to be of type number | undefined, but I'm seeing string | number | boolean | undefined:

Do you think the type can be updated to support that case ?

For now, I'll cast the type

    const { shadowGid, shadowX, shadowY, tintcolor, opacity } = mergeAll(
      properties
        .filter((p) => p.type === "int")
        .map((p) => ({ [p.name]: p.value }))
    ) as Record<string, number>;

from tiled-types.

Related Issues (7)

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.