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

Suspecious about same values check in Typescript #56556

Closed
sadiki-o opened this issue Nov 27, 2023 · 2 comments
Closed

Suspecious about same values check in Typescript #56556

sadiki-o opened this issue Nov 27, 2023 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@sadiki-o
Copy link

🔎 Search Terms

Typescript checking same parameters
Dynamic vs Static typing Typescript

🕗 Version & Regression Information

Property 'group' does not exist on type 'string | { type: "group"; columnIndex: number; group: (string | { type: "operation"; columnType: Primitive; value: string[]; })[]; }'.
Property 'group' does not exist on type 'string'.

⏯ Playground Link

https://www.typescriptlang.org/play?#code/C4TwDgpgBACgTgSwLYOAgbtAvFA5AOwFckAjCOXKAHzwGdhF8BzSm3Egew4BsIBDfLgDcAKBGhIUAMrIwvAIqFyIACrhsUABQiou6lHqMmOvTQDeJvXokQAXHiZwOhMMMtWoAYx7F8ASXwAEwgAD3siUnJRDz1HZzB7bRiYmkMEZndk-QssrJt7XA5IOD40DkFo3Jjvbl81SHt4ZFQMCEqqq3Q+WrsDBnSmAG0AXXbcgF9MvQBKEbHJ2dGxb3x6KABHJTgQexkkOQhFZXqNEbERFbWXQNKIADVupSgsd010ezTmABooCE3uj79b5QdLBMJQQYRMhwH5Q8jDab2AQgZ4APigFimugQADMtFiPDYOHjNspBqDQoMAAzDYZQACEWBwuE+LCgADJ2QSrKTtuSgpSacMAHRxFz8sGDACMtIJ0wx3L0cAgwEIcHwUAAtFKxlZJnrluUrmAbsB7o8IAB9Z6vd59Iw-P6EAH2gY-CngyHEaGw73wxFQZFohVZXH4jpQIkkrYgQbUn4y6m0hlMuhAtmcxW6Xmx+NQRNC0VOcUe6WyrLynIdZWq9VanUE-V6SYXI3AKDXW4PHqW60vKxvQEO37-bhDt0ggWeuEwqAzhFI-AorDoqvVNsGPhIc1jud+uDPKAAOX3mip011ejDSQ6UY2McGtC3O7pjOZrMomYjOcfz+7IrFMAJUpGVhjlENqxVNUNW1S9dCbeDDVWdtOzNbslF7PtbXHYEnRdVl3SnewvUiWd5wDIMVwg9dkM3bdu3CfdDxPUjNFLGkLwJa8s0jdRiXvMkn3o7pX1TFl00-LlvwfISXyLeJgJCMswIrajchraD6zgqAEJ0pDjVNHcMMwm0BztAiR2dXcLI9YiZ19UiF0DJdg0xUM8RvKo7x-GkU3fCSOSkjofIA4sgNLUDwLXdSoLrWDG3ccYgA

💻 Code

type Primitive = 'number' | 'string' | 'boolean';

type SimpleQueryType = (
    | string
    | {
        type: 'group';
        columnIndex: number;
        group: (
            | string
            | {
                type: 'operation';
                columnType: Primitive;
                value: string[];
            }
        )[];
    }
)[];

const query: SimpleQueryType = []


const updateValue =
    (v: string, equal: string, index: [number, number]): any => {

        if (
            typeof query[index[0]] !== 'string' &&
            query[index[0]].group[index[1]]
        ) {
            return -1;
        }
    }

const updateValue_ =
    (v: string, equal: string, index: [number, number]): any => {
        if (
            typeof query[[0, 1][0]] !== 'string' &&
            query[[0, 1][0]].group[index[1]]
        ) {
            return -1;
        }
    }

const updateValue__ =
    (v: string, equal: string, index: [number, number]): any => {
        const sameVal: number = Number(0);
        if (
            typeof query[sameVal] !== 'string' &&
            query[sameVal].group[index[1]]
        ) {
            return -1;
        }
    }

const updateValue___ =
    (v: string, equal: string, index: [number, number]): any => {
        const sameVal: number = Number(index[0]);
        if (
            typeof query[sameVal] !== 'string' &&
            query[sameVal].group[index[1]]
        ) {
            return -1;
        }
    }

🙁 Actual behavior

This code is weird because passing the same values as array index , Typescript consider them to be different (I understand how arrays are passed by reference so the array could get modified by the same the first check and the second check use that index). The only way Typescript consider those two values the same is when you pass static values . Passing Number(0) also doesn't work !!

🙂 Expected behavior

It should consider passing the same variable at least (instead of array values) make the check pass:

const updateValue___ =
    (v: string, equal: string, index: [number, number]): any => {

        if (
            typeof query[0] !== 'string' &&
            query[0].group[index[1]]
        ) {
            return -1;
        }
    }

Additional information about the issue

I may be wrong on how Typescript handle such cases so sorry in advance if i mis-understand things

@MartinJohns
Copy link
Contributor

Duplicate of #56389.

@andrewbranch andrewbranch added the Duplicate An existing issue was already created label Nov 27, 2023
@typescript-bot
Copy link
Collaborator

This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Nov 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants