how can I limit property when type casting (typescript)How to get a subset of a javascript object's propertiesHow do you explicitly set a new property on `window` in TypeScript?Type definition in object literal in TypeScriptAre strongly-typed functions as parameters possible in TypeScript?How do I cast a JSON object to a typescript classDoes TypeScript infer castings when assigning anonymous objects to a typed property?How to type Redux actions and Redux reducers in TypeScript?Typescript: Interfaces vs TypesHow to define a private property when implementing an interface in Typescript?Casting a function parameter object property in TypescriptHow can I check that a string is a property a particular interface in TypeScript

Pre-1972 sci-fi short story or novel: alien(?) tunnel where people try new moves and get destroyed if they're not the correct ones

SQL counting distinct over partition

How can electric fields be used to detect cracks in metals?

Impedance ratio vs. SWR

Fixing obscure 8080 emulator bug?

How can "научись" mean "take it and keep trying"?

How does an ordinary object become radioactive?

What is the `some` keyword in SwiftUI?

A curious prime counting approximation or just data overfitting?

Why did the Herschel Space Telescope need helium coolant?

Why is one of Madera Municipal's runways labelled with only "R" on both sides?

Medieval flying castle propulsion

SOQL Not Recognizing Field?

Did Milano or Benatar approve or comment on their namesake MCU ships?

Cycle through MeshStyle directives in ListLinePlot

Passing multiple files through stdin (over ssh)

Generate a Graeco-Latin square

Arriving at the same result with the opposite hypotheses

Preventing employees from either switching to competitors or opening their own business

Compiling C files on Ubuntu and using the executable on Windows

Grover algorithm for a database search: where is the quantum advantage?

How to return a security deposit to a tenant

Should I give professor gift at the beginning of my PhD?

How to signal to my players that the following part is supposed to be played on fast forward?



how can I limit property when type casting (typescript)


How to get a subset of a javascript object's propertiesHow do you explicitly set a new property on `window` in TypeScript?Type definition in object literal in TypeScriptAre strongly-typed functions as parameters possible in TypeScript?How do I cast a JSON object to a typescript classDoes TypeScript infer castings when assigning anonymous objects to a typed property?How to type Redux actions and Redux reducers in TypeScript?Typescript: Interfaces vs TypesHow to define a private property when implementing an interface in Typescript?Casting a function parameter object property in TypescriptHow can I check that a string is a property a particular interface in TypeScript






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















How can I get expected result and what's the best approach to limit property when type casting ??



interface user 
name: string
password: string


interface userEntity
name: string


const user: user =
name: 'name',
password: 'password'


const result = user as userEntity;
// output name: 'name', password: 'password'
// expected name: 'name'









share|improve this question






















  • What the real case here? Force casting is not good practice. And even if it was ok, what is the difference between the "user entity" and the "user"? Please give a more real-life scenario

    – MCMatan
    Mar 24 at 17:24











  • the user is a model get from database ORM, then I wanna response to frontend exclude password. Actually, I can new a variable exclude password for the response, but I don't think it is a good approach.

    – Tung Tse
    Mar 24 at 18:51











  • It sounds like a better approach to me. As a rule of thumb, any time you are force casting, you probably are doing something wrong. You can have a dedicated constructor that accepts User input

    – MCMatan
    Mar 24 at 18:54

















0















How can I get expected result and what's the best approach to limit property when type casting ??



interface user 
name: string
password: string


interface userEntity
name: string


const user: user =
name: 'name',
password: 'password'


const result = user as userEntity;
// output name: 'name', password: 'password'
// expected name: 'name'









share|improve this question






















  • What the real case here? Force casting is not good practice. And even if it was ok, what is the difference between the "user entity" and the "user"? Please give a more real-life scenario

    – MCMatan
    Mar 24 at 17:24











  • the user is a model get from database ORM, then I wanna response to frontend exclude password. Actually, I can new a variable exclude password for the response, but I don't think it is a good approach.

    – Tung Tse
    Mar 24 at 18:51











  • It sounds like a better approach to me. As a rule of thumb, any time you are force casting, you probably are doing something wrong. You can have a dedicated constructor that accepts User input

    – MCMatan
    Mar 24 at 18:54













0












0








0








How can I get expected result and what's the best approach to limit property when type casting ??



interface user 
name: string
password: string


interface userEntity
name: string


const user: user =
name: 'name',
password: 'password'


const result = user as userEntity;
// output name: 'name', password: 'password'
// expected name: 'name'









share|improve this question














How can I get expected result and what's the best approach to limit property when type casting ??



interface user 
name: string
password: string


interface userEntity
name: string


const user: user =
name: 'name',
password: 'password'


const result = user as userEntity;
// output name: 'name', password: 'password'
// expected name: 'name'






typescript type-conversion






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 24 at 17:15









Tung TseTung Tse

3014




3014












  • What the real case here? Force casting is not good practice. And even if it was ok, what is the difference between the "user entity" and the "user"? Please give a more real-life scenario

    – MCMatan
    Mar 24 at 17:24











  • the user is a model get from database ORM, then I wanna response to frontend exclude password. Actually, I can new a variable exclude password for the response, but I don't think it is a good approach.

    – Tung Tse
    Mar 24 at 18:51











  • It sounds like a better approach to me. As a rule of thumb, any time you are force casting, you probably are doing something wrong. You can have a dedicated constructor that accepts User input

    – MCMatan
    Mar 24 at 18:54

















  • What the real case here? Force casting is not good practice. And even if it was ok, what is the difference between the "user entity" and the "user"? Please give a more real-life scenario

    – MCMatan
    Mar 24 at 17:24











  • the user is a model get from database ORM, then I wanna response to frontend exclude password. Actually, I can new a variable exclude password for the response, but I don't think it is a good approach.

    – Tung Tse
    Mar 24 at 18:51











  • It sounds like a better approach to me. As a rule of thumb, any time you are force casting, you probably are doing something wrong. You can have a dedicated constructor that accepts User input

    – MCMatan
    Mar 24 at 18:54
















What the real case here? Force casting is not good practice. And even if it was ok, what is the difference between the "user entity" and the "user"? Please give a more real-life scenario

– MCMatan
Mar 24 at 17:24





What the real case here? Force casting is not good practice. And even if it was ok, what is the difference between the "user entity" and the "user"? Please give a more real-life scenario

– MCMatan
Mar 24 at 17:24













the user is a model get from database ORM, then I wanna response to frontend exclude password. Actually, I can new a variable exclude password for the response, but I don't think it is a good approach.

– Tung Tse
Mar 24 at 18:51





the user is a model get from database ORM, then I wanna response to frontend exclude password. Actually, I can new a variable exclude password for the response, but I don't think it is a good approach.

– Tung Tse
Mar 24 at 18:51













It sounds like a better approach to me. As a rule of thumb, any time you are force casting, you probably are doing something wrong. You can have a dedicated constructor that accepts User input

– MCMatan
Mar 24 at 18:54





It sounds like a better approach to me. As a rule of thumb, any time you are force casting, you probably are doing something wrong. You can have a dedicated constructor that accepts User input

– MCMatan
Mar 24 at 18:54












1 Answer
1






active

oldest

votes


















1














Type casting has no effect at runtime, the values in object will remain the same. Type casting only purpose is to suppress compilation errors when type-checking, when you know better than the compiler what the actual type of the object should be.



To copy only a subset of properties when assigning a value to another object, you have to do it explicitly at runtime. There is a multitude of possible ways to do that, for example



const result: userEntity = name: user.name;





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55326393%2fhow-can-i-limit-property-when-type-casting-typescript%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Type casting has no effect at runtime, the values in object will remain the same. Type casting only purpose is to suppress compilation errors when type-checking, when you know better than the compiler what the actual type of the object should be.



    To copy only a subset of properties when assigning a value to another object, you have to do it explicitly at runtime. There is a multitude of possible ways to do that, for example



    const result: userEntity = name: user.name;





    share|improve this answer



























      1














      Type casting has no effect at runtime, the values in object will remain the same. Type casting only purpose is to suppress compilation errors when type-checking, when you know better than the compiler what the actual type of the object should be.



      To copy only a subset of properties when assigning a value to another object, you have to do it explicitly at runtime. There is a multitude of possible ways to do that, for example



      const result: userEntity = name: user.name;





      share|improve this answer

























        1












        1








        1







        Type casting has no effect at runtime, the values in object will remain the same. Type casting only purpose is to suppress compilation errors when type-checking, when you know better than the compiler what the actual type of the object should be.



        To copy only a subset of properties when assigning a value to another object, you have to do it explicitly at runtime. There is a multitude of possible ways to do that, for example



        const result: userEntity = name: user.name;





        share|improve this answer













        Type casting has no effect at runtime, the values in object will remain the same. Type casting only purpose is to suppress compilation errors when type-checking, when you know better than the compiler what the actual type of the object should be.



        To copy only a subset of properties when assigning a value to another object, you have to do it explicitly at runtime. There is a multitude of possible ways to do that, for example



        const result: userEntity = name: user.name;






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 24 at 19:33









        artemartem

        16.9k33345




        16.9k33345





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55326393%2fhow-can-i-limit-property-when-type-casting-typescript%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

            Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript