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

Type checking shows unexpected behavior when a function argument is defined with a partial object. #60543

Open
umenosuke opened this issue Nov 20, 2024 · 2 comments

Comments

@umenosuke
Copy link

πŸ”Ž Search Terms

function argument
partial object
index signatures

πŸ•— Version & Regression Information

What I've tried in the Playground

5.8.0-dev.20241119
5.6.3 <-- This is my development environment version
3.3.3

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.8.0-dev.20241119#code/N4KABBYGYK4HYGMAuBLA9nMSCmBnJAFAIYBOA5rgFxiiR1gAWaZ2TLA-NfiSnGQNzhIAXwCUNIfTAIMuNABtsAOnnNi5XKMGT6Ael1hsADwAO2ZGAC8APkbNW9sClxgC3XmTAAfMPAAm2FC82H6iOnQycHKKKmqkFEpsDixa4ZCR0cqqZARIAJ5maFBg8biJ9kmpdMIgOjj4BLRSdixJ1ABERF1E7QA0OmLadE1SGUgl1MBgANoA1th5XEg8fAC61HAwALYARtgkYMJWEs307UlJ7dQAjAAM-ad07dtEL1dgdw-NwkPN+k7jVRoWYuGwtZLYJwuAibXb7by+OABIJwEJhU4ZBRZOLTc4VeztVZVZqYmLZXIFbBFEq4i4EomCU5pCD-ADCGGWpDyWDQhlM5iQRFQsl6YDgvP2JDQBzQCAQMBIuGZYH+AGUiFtIQB3FBIBhgdrigAKUrMJHyAEE5XhcAAxKVbACSSOMqpQZDgQoV2HaYFw2HGSF57SgRHk-vayvqhCIxIgNR0I3oYwmYAASuZpX4ADzuPii2F7Ei2SwnR4QPGtAk3e7Kp4vN41r5SH61U7-XVgIEgqy2OksKGuQvwnz+QLBULK0nYnJEWn4liEuPJ2RY2I5fKFYpzysQpeM5rKtkckhcnl8szIIXoKIFiUkKUyuUKpWnaPEZcJ4ZT2TjIjHJNTl3S4mzrSBni2V5IPeT5lVbH8ojXckd37H0GQQzJ1wpLcaWA+lUiPAxHWKPVIRMU19nyMUNUhZw-UvFAghCC95BQBBdXkPJRVIwwH2lMBZXlRUShcYxLxwPwlCjPAY0-EAaiAA

πŸ’» Code

{
    function test(args: {
        hogehoge?: string;
    }) {
        console.log(args);

        // expect => hogehoge is (string | undefined)
        console.log(args.hogehoge);
        console.log(typeof args.hogehoge);
    }

    test({
        hogehoge: "aaaa",
    });

    {
        const a: { [key: string]: number } = {
            "hogehoge": 10,
            "umauma": 10,
        };

        // it looks => hogehoge is (number | undefined)
        console.log(a["hogehoge"]);
        console.log(typeof a["hogehoge"]);
        
        // Contrary to expectations, no error occurs
        // Same with "noPropertyAccessFromIndexSignature" set to "false"
        test(a);
    }

    {
        const a: Record<string, number> = {
            "hogehoge": 10,
            "umauma": 10,
        };

        // it looks => hogehoge is (number | undefined)
        console.log(a["hogehoge"]);
        console.log(typeof a["hogehoge"]);
        
        // Contrary to expectations, no error occurs
        test(a);
    }

    {
        const a = {
            "hogehoge": 10,
            "umauma": 10,
        };

        console.log(a["hogehoge"]);
        console.log(typeof a["hogehoge"]);

        // If the property name is specified explicitly, the error occurs as expected.
        test(a);
    }
}

πŸ™ Actual behavior

See code comments above.

πŸ™‚ Expected behavior

See code comments above.

Additional information about the issue

When specifying a partial object as a function argument,
it is possible to call the function with variables defined by index signatures
(different types than the function argument).

Below is the tsconfig.json I am using

{
    "compilerOptions": {
        "declaration": true,
        "forceConsistentCasingInFileNames": true,
        "module": "ES2022",
        "noFallthroughCasesInSwitch": true,
        "noImplicitOverride": true,
        "noImplicitReturns": true,
        "noPropertyAccessFromIndexSignature": true,
        "noUncheckedIndexedAccess": true,
        "outDir": "./contents/build/",
        "removeComments": true,
        "rootDir": "./contents/src/",
        "skipLibCheck": true,
        "sourceMap": true,
        "strict": true,
        "target": "ES2022",
        "verbatimModuleSyntax": true
    }
}
@jcalz
Copy link
Contributor

jcalz commented Nov 20, 2024

Duplicate of #27144

@umenosuke
Copy link
Author

Thank you, it certainly seems similar to that issue.
I will ask questions there from now on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants