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;
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
add a comment |
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
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
add a comment |
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
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
typescript type-conversion
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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;
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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;
add a comment |
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;
add a comment |
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;
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;
answered Mar 24 at 19:33
artemartem
16.9k33345
16.9k33345
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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