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

[BUG] Union properties are broken #83

Open
AntonC9018 opened this issue Nov 20, 2022 · 5 comments
Open

[BUG] Union properties are broken #83

AntonC9018 opened this issue Nov 20, 2022 · 5 comments
Labels
bug Something isn't working enhancement New feature or request

Comments

@AntonC9018
Copy link

AntonC9018 commented Nov 20, 2022

Consider the following code:

import {getType,Type} from "tst-reflect";
class A
{
    s?: string | undefined;
} 
console.log(getType<number|string>().isUnion());
console.log(getType<A>().getProperties().find(p => p.name == "s")!.type.isUnion());

The first check works fine and returns true.
In the second check, the type of s is clearly a union, but it returns false.
The same thing happens if I try to extract the types — the first statement correctly returns the two types, while the second one sometimes returns an empty array and sometimes return undefined.
I'm using Vite with HMR (btw the hot reloading doesn't trigger when the transformed input changes, it may or may not be a problem on your side of things).

@AntonC9018 AntonC9018 added bug Something isn't working enhancement New feature or request labels Nov 20, 2022
@Hookyns
Copy link
Owner

Hookyns commented Nov 21, 2022

Hi,
do you have strictNullChecks option in tsconfig enabled?

@Hookyns
Copy link
Owner

Hookyns commented Nov 21, 2022

Check the issue #45

@aj-bartocci
Copy link

aj-bartocci commented Nov 21, 2022

I'm similarly seeing some weirdness when using optional properties or unions. However isUnion() does appear to be returning correct values for me. However the type itself is being shown as a container which I'm not sure if that is intended.

I have strictNullChecks enabled.

import { getType } from "tst-reflect";

function printTypeProperties<TType>() {
    const type = getType<TType>(); // <<== get type of type parameter TType
    
    console.log(type.getProperties().map(prop => prop.name + ": " + prop.type.name + `, isOptional: ${prop.optional}` + `, isUnion: ${prop.type.isUnion()}` + `, type: ${prop.type}`).join("\n"));
}

interface SomeType {
    foo: string;
    bar?: number;
    baz: boolean | undefined;
}

printTypeProperties<SomeType>();
/*
Prints: 
foo: String, isOptional: false, isUnion: false, type: {Native String (String)}
bar: bar, isOptional: true, isUnion: true, type: {Container bar (bar)}
baz: baz, isOptional: false, isUnion: true, type: {Container baz (baz)}

*/

I was hoping the that the type name for bar would be something like Optional String and baz would be something like Union boolean, undefined

Sorry if I'm hijacking the thread it just seemed like the issues might be related. I can open a new issue if that would be better.

@Hookyns
Copy link
Owner

Hookyns commented Nov 21, 2022

@aj-bartocci
Container is meant to be a wrapper for all types consisting of multiple other types, such as Union, Intersection and Enum (which is union), but it wasn't good design decision so it is fixed in the new major version (not published yet) - Union, Intersection, Enum etc have own types.

Each container type has multiple types inside. You can access them via types property, eg. prop.type.types.

About naming... Class has name, interface has name, function has name, type alias has name but other types (such as types of properties, fields, parameters etc.) don't have names in TS. So names of those containers are not intended.

@aj-bartocci
Copy link

Got it that makes sense. Using prop.type.types should fit my needs. Thank you for creating this library by the way it's very cool.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants