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

What is the detail logic of type assertion? #23698

Closed
chinesedfan opened this issue Apr 26, 2018 · 6 comments
Closed

What is the detail logic of type assertion? #23698

chinesedfan opened this issue Apr 26, 2018 · 6 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@chinesedfan
Copy link

TypeScript Version: 2.8.1

Search Terms: type assertion

Code

interface PM_MGEInfo {
    category: string;
    bid: string;
    cid?: string;
    labs?: { [key: string]: any };
}

function makeMgeInfo1(bid: string): PM_MGEInfo {
    return <PM_MGEInfo>{
        bid
    };
}

function makeMgeInfo2(bid: string): PM_MGEInfo {
    return <PM_MGEInfo>{
        bid,
        labs: {}
    };
}

function makeMgeInfo3(bid: string): PM_MGEInfo {
    return <PM_MGEInfo>{
        bid,
        // error TS2352: Type '{ labs: { poi_id: string; }; bid: string; }' cannot be converted to type 'PM_MGEInfo'.
        // Property 'category' is missing in type '{ labs: { poi_id: string; }; bid: string; }'.
        labs: {a: 1}
    };
}

Expected behavior:
The compiler always complains the missing of field category, or never. I know type assertion is not 100% safe, but am confused about its check logic. Can someone point out related codes for me? The ts code base is so complex.

Actual behavior:
Only makeMgeInfo3 throws an error about the missing of field category.

Playground Link: very long link

Related Issues:
I also asked on stackoverflow, but got no satisfying answers.

@mhegazy
Copy link
Contributor

mhegazy commented Apr 26, 2018

Docs are at: https://www.typescriptlang.org/docs/handbook/basic-types.html#type-assertions

Basically the compiler is checking that the two types are somehow related, that is that one is assignable to the other. In this case neither is the case. In one way the object literal is missing a a required property category, in the other way missing required property labs.a

@chinesedfan
Copy link
Author

In this case neither is the case. In one way the object literal is missing a a required property category, in the other way missing required property labs.a

@mhegazy Can you explain a little more about why case 1&2 are no compilation errors? What's the difference between 1/2 and 3?

@mhegazy
Copy link
Contributor

mhegazy commented Apr 26, 2018

Cuase they are assignable in one direction.

@chinesedfan
Copy link
Author

chinesedfan commented Apr 26, 2018

@mhegazy Sorry, I still don't understand. I think the 3 object literals are not assignable to PM_MGEInfo, due to the lack of property category. But why does only the last case trigger the check?

If it is hard to explain, can you give me a link to the related TypeScript source codes?

@DanielRosenwasser
Copy link
Member

There's a function in src/compiler/checker.ts called checkAssertion. I can't link it here because the file's too big.

@DanielRosenwasser DanielRosenwasser added the Question An issue which isn't directly actionable in code label Apr 26, 2018
@typescript-bot
Copy link
Collaborator

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@microsoft microsoft locked and limited conversation to collaborators Jul 31, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

4 participants