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

@serializable: Error squiggles in VSCode #179

Closed
6 tasks done
krisrok opened this issue Sep 11, 2023 · 9 comments
Closed
6 tasks done

@serializable: Error squiggles in VSCode #179

krisrok opened this issue Sep 11, 2023 · 9 comments
Labels
fixed → to verify but needs testing

Comments

@krisrok
Copy link
Contributor

krisrok commented Sep 11, 2023

Describe the bug 💬

VSCode is complaining about usage of @serializable on non-primitive targets like Object3D:
image

This renders almost every file/tab with red font because it has this error.

Changing the returned function to also accept any and transforming it to a string if necessary seems to work:

    return function (_target: any, _propertyKey: string | any) {
        // this is important so objects with inheritance dont override their serialized type 
        // info if e.g. multiple classes inheriting from the same type implement a member with the same name
        // and both use @serializable() with different types 
        if(typeof(_propertyKey) != 'string')
            _propertyKey = _propertyKey.toString();
        if (!Object.getOwnPropertyDescriptor(_target, '$serializedTypes'))
            _target["$serializedTypes"] = {};
        const types = _target["$serializedTypes"] = _target["$serializedTypes"] || {}
        types[_propertyKey] = type;
    }

Squiggle gone, yay:
image

Operating System 👩‍💻

Windows

What browsers are you seeing the problem on? 🏄‍♂️

No response

Editor Version 🎲

2020.3

Needle Exporter version

3.13.0-pre

Project Info (Unity only) 📜

No response

Steps to reproduce 🔢

1. Open a .ts class using @serializable

Reproduction ♻

No response

Discord or Discussion link

No response

Validations 🩹

@krisrok krisrok added the untriaged Newly reported issue label Sep 11, 2023
@marwie
Copy link
Member

marwie commented Sep 11, 2023

Hey, which version of typescript are you using?

@krisrok
Copy link
Contributor Author

krisrok commented Sep 11, 2023

Oh is this a fault on my end only? Sorry, I'm new to this whole ecosystem.

Anyway, this is what i found:

> npm ls typscript
my-needle-engine-project@1.0.0
├─┬ @needle-tools/helper@1.1.0
│ └─┬ @needle-tools/needle-component-compiler@1.10.3
│   └── typescript@4.9.5
└── typescript@5.2.2

@marwie
Copy link
Member

marwie commented Sep 11, 2023

Thanks, I havent tested it with typescript 5 yet.

Can you try if changing the type to string | symbol works for you as well?
https://devblogs.microsoft.com/typescript/announcing-typescript-5-0/#more-accurate-type-checking-for-parameter-decorators-in-constructors-under-experimentaldecorators

@krisrok
Copy link
Contributor Author

krisrok commented Sep 11, 2023

Thanks, I havent tested it with typescript 5 yet.

Oh? That's what gets installed automatically using the exporter though? I did not (knowingly) deviate from the defaults.

Can you try if changing the type to string | symbol works for you as well? https://devblogs.microsoft.com/typescript/announcing-typescript-5-0/#more-accurate-type-checking-for-parameter-decorators-in-constructors-under-experimentaldecorators

Nope:
image

@marwie
Copy link
Member

marwie commented Sep 11, 2023

Ah sorry I wasnt aware that it was updated in the vite template. So that can't be it

What do you get when you change it to any to make typescript happy and if(typeof _propertyKey !== "string") console.log(typeof _propertyKey, _propertyKey) ?

@krisrok
Copy link
Contributor Author

krisrok commented Sep 11, 2023

Interesting: Does not log anything. I can set a breakpoint there and it hits (after un-ingoring the file). Is there some magic going on?

@marwie
Copy link
Member

marwie commented Sep 11, 2023

Ah you need to edit the file in lib/engine (the .js file) and not in src/engine

@hybridherbst
Copy link
Contributor

hybridherbst commented Sep 13, 2023

Looks like in TS5 this parameter is a ClassFieldDecoratorContext instead of a string. My understanding would be that there's different TS versions conflicting, one with a config of experimentalDecorators (like we're using, with the string parameter) and one without.

This issue would likely go away if we switch to TS5 entirely and drop support for experimentalDecorators, instead using the non-experimental ones with that context.

I'm not sure if we can use ClassFieldDecoratorContext as alternative type or if that's just not available in TS4, which would make it seem like "any" and casting to string is the right way for now...

image

Here are some decorator usage notes for TS5:
https://devblogs.microsoft.com/typescript/announcing-typescript-5-2-beta/#decorator-metadata

https://github.com/microsoft/TypeScript/blob/3ade5022d7f287fdaf61c9e6deed32a3ffb60bc4/src/lib/decorators.d.ts#L326

And the info in the PR that added it is much better than the current docs:
microsoft/TypeScript#50820

@marwie
Copy link
Member

marwie commented Sep 13, 2023

We'll add a fix for this in the next update by supporting both the old and the new decorator syntax:

_propertyKey: string | { name: string }

a call to toString() in this case doesn't make sense since we need the decorated field/property name here. The new method argument (ClassFieldDecoratorContext) has that inside the name property so we have to properly handle both types and extract the information necessary.

That being said our package will continue using experimentalDecorators as long as it's officially supported by typescript to not break backwards compatibility - according to their announcement it won't be removed anytime soon.

@marwie marwie added fixed → to verify but needs testing and removed untriaged Newly reported issue labels Sep 13, 2023
@marwie marwie closed this as completed Sep 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fixed → to verify but needs testing
Projects
None yet
Development

No branches or pull requests

3 participants