Object.keys for a non-instantiated object interface typeEnforcing the type of the indexed members of a Typescript object?Interface type check with TypescriptTypescript interface default valuesTypescript: Interfaces vs TypesTypescript type mapping to expect an object where every property is of a given typeExtending Non Generic Interface with Generic InterfaceTypescript - Force default type for additional properties in interfaceInstantiating an exported TypeScript interfaceDefining an interface for an object which is extended by another interfaceUse an enum to type an index signature of an interface field?
What does “studies need to be taken with more than the usual grain of salt” mean?
What happens to extra attacks after you kill your declared target
Substantivization of "continuum"
What is this second smaller runway next to London City Airport?
Is it possible to commute 34 km (21 miles) daily?
How much caffeine would there be if I reuse tea leaves in a second brewing?
What's the most profitable use for an elemental transmuter?
Temporarily modify the way a counter is displayed in an existing environment
How to answer to the "We do not want to create any precedent" argument in salary negotiation?
Right Ascension for epoch 2000 - physical location?
For a command to increase something, should instructions refer to the "+" key or the "=" key?
Command which removes data left side of ";" (semicolon) on each row
How to tell my research advisor I'd like to drop a class?
How Can I Get A LaTex Table Generated By R's MonteCarlo Package To Work?
How does AT-AT deploy troops?
Can you put L trominos to fill the figure?
Why does the SR-71 Blackbird sometimes have dents in the nose?
What could a technologically advanced but outnumbered alien race do to destroy humanity?
Artificially isolated pawn in the Caro-Kann
Equivalent of phrase 'emu parade' in other English-speaking places
What term can we propose for someone who prioritizes equipment over musicianship?
L tromino tiling
Moisture leaking out of chip in floor tile
Did the Allies reverse the threads on secret microfilm-hiding buttons to thwart the Germans?
Object.keys for a non-instantiated object interface type
Enforcing the type of the indexed members of a Typescript object?Interface type check with TypescriptTypescript interface default valuesTypescript: Interfaces vs TypesTypescript type mapping to expect an object where every property is of a given typeExtending Non Generic Interface with Generic InterfaceTypescript - Force default type for additional properties in interfaceInstantiating an exported TypeScript interfaceDefining an interface for an object which is extended by another interfaceUse an enum to type an index signature of an interface field?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty
margin-bottom:0;
Is there a way to use Object.keys on an interface type itself to get all of the properties of that object type?
export interface Apple
id: number;
name: string;
status: string;
Object.keys(Apple)
Exepected output would be ["id", "name", "status"] like the normal Object.keys. I can't seem to find a way or documentation to accomplish this.
typescript
add a comment
|
Is there a way to use Object.keys on an interface type itself to get all of the properties of that object type?
export interface Apple
id: number;
name: string;
status: string;
Object.keys(Apple)
Exepected output would be ["id", "name", "status"] like the normal Object.keys. I can't seem to find a way or documentation to accomplish this.
typescript
2
As Sam Herrmann mentioned, it sounds impossible. Why do you want to do this?
– Dakota Jang
Mar 28 at 21:47
I wanted to set a constant at the top of my file that has all the properties of the interface. I iterate through them for the diffing tool I am writing, so I wanted to keep them dynamic in case any properties got added or deleted.
– C.Programming
Mar 28 at 21:52
Isn't that the reason why we use Typescript?class Foo implements Bar
would complain ifFoo
is missing some implementations inBar
. If you want to prevent modification in runtime, you could tryObject.freeze
. Though I have a feeling that's not exactly something you are looking for.
– Dakota Jang
Mar 31 at 2:08
add a comment
|
Is there a way to use Object.keys on an interface type itself to get all of the properties of that object type?
export interface Apple
id: number;
name: string;
status: string;
Object.keys(Apple)
Exepected output would be ["id", "name", "status"] like the normal Object.keys. I can't seem to find a way or documentation to accomplish this.
typescript
Is there a way to use Object.keys on an interface type itself to get all of the properties of that object type?
export interface Apple
id: number;
name: string;
status: string;
Object.keys(Apple)
Exepected output would be ["id", "name", "status"] like the normal Object.keys. I can't seem to find a way or documentation to accomplish this.
typescript
typescript
asked Mar 28 at 21:38
C.ProgrammingC.Programming
387 bronze badges
387 bronze badges
2
As Sam Herrmann mentioned, it sounds impossible. Why do you want to do this?
– Dakota Jang
Mar 28 at 21:47
I wanted to set a constant at the top of my file that has all the properties of the interface. I iterate through them for the diffing tool I am writing, so I wanted to keep them dynamic in case any properties got added or deleted.
– C.Programming
Mar 28 at 21:52
Isn't that the reason why we use Typescript?class Foo implements Bar
would complain ifFoo
is missing some implementations inBar
. If you want to prevent modification in runtime, you could tryObject.freeze
. Though I have a feeling that's not exactly something you are looking for.
– Dakota Jang
Mar 31 at 2:08
add a comment
|
2
As Sam Herrmann mentioned, it sounds impossible. Why do you want to do this?
– Dakota Jang
Mar 28 at 21:47
I wanted to set a constant at the top of my file that has all the properties of the interface. I iterate through them for the diffing tool I am writing, so I wanted to keep them dynamic in case any properties got added or deleted.
– C.Programming
Mar 28 at 21:52
Isn't that the reason why we use Typescript?class Foo implements Bar
would complain ifFoo
is missing some implementations inBar
. If you want to prevent modification in runtime, you could tryObject.freeze
. Though I have a feeling that's not exactly something you are looking for.
– Dakota Jang
Mar 31 at 2:08
2
2
As Sam Herrmann mentioned, it sounds impossible. Why do you want to do this?
– Dakota Jang
Mar 28 at 21:47
As Sam Herrmann mentioned, it sounds impossible. Why do you want to do this?
– Dakota Jang
Mar 28 at 21:47
I wanted to set a constant at the top of my file that has all the properties of the interface. I iterate through them for the diffing tool I am writing, so I wanted to keep them dynamic in case any properties got added or deleted.
– C.Programming
Mar 28 at 21:52
I wanted to set a constant at the top of my file that has all the properties of the interface. I iterate through them for the diffing tool I am writing, so I wanted to keep them dynamic in case any properties got added or deleted.
– C.Programming
Mar 28 at 21:52
Isn't that the reason why we use Typescript?
class Foo implements Bar
would complain if Foo
is missing some implementations in Bar
. If you want to prevent modification in runtime, you could try Object.freeze
. Though I have a feeling that's not exactly something you are looking for.– Dakota Jang
Mar 31 at 2:08
Isn't that the reason why we use Typescript?
class Foo implements Bar
would complain if Foo
is missing some implementations in Bar
. If you want to prevent modification in runtime, you could try Object.freeze
. Though I have a feeling that's not exactly something you are looking for.– Dakota Jang
Mar 31 at 2:08
add a comment
|
2 Answers
2
active
oldest
votes
This is not possible because TypeScript is only present at development time. Once your code is transpiled to JavaScript, there is no way to reference back to TypeScript types at runtime.
That's a bummer. But makes sense. I guess I will have to hardcode the property names and just update it manually if there are any changes. Thank you!
– C.Programming
Mar 28 at 21:52
add a comment
|
From documentation I see the following for return value of Object.keys()
:
An array of strings that represent all the enumerable properties of the given object.
Source: Object.keys().
Which means for example if you have a class what you instantiate from JavaScript then you can get the keys into an array but not from the interface (from TypeScript):
let redApple = new Apple();
let keys = Object.keys(redApple);
let classKeys = Object.keys(Apple);
console.log('keys of the instance', keys);
console.log('keys of the class', classKeys);
function Apple()
this.id = '';
this.name = '';
this.status = '';
We should remember always that at the end TypeScript will be transpiled to JavaScript.
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/4.0/"u003ecc by-sa 4.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%2f55407235%2fobject-keys-for-a-non-instantiated-object-interface-type%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
This is not possible because TypeScript is only present at development time. Once your code is transpiled to JavaScript, there is no way to reference back to TypeScript types at runtime.
That's a bummer. But makes sense. I guess I will have to hardcode the property names and just update it manually if there are any changes. Thank you!
– C.Programming
Mar 28 at 21:52
add a comment
|
This is not possible because TypeScript is only present at development time. Once your code is transpiled to JavaScript, there is no way to reference back to TypeScript types at runtime.
That's a bummer. But makes sense. I guess I will have to hardcode the property names and just update it manually if there are any changes. Thank you!
– C.Programming
Mar 28 at 21:52
add a comment
|
This is not possible because TypeScript is only present at development time. Once your code is transpiled to JavaScript, there is no way to reference back to TypeScript types at runtime.
This is not possible because TypeScript is only present at development time. Once your code is transpiled to JavaScript, there is no way to reference back to TypeScript types at runtime.
answered Mar 28 at 21:42
Sam HerrmannSam Herrmann
2,04210 silver badges26 bronze badges
2,04210 silver badges26 bronze badges
That's a bummer. But makes sense. I guess I will have to hardcode the property names and just update it manually if there are any changes. Thank you!
– C.Programming
Mar 28 at 21:52
add a comment
|
That's a bummer. But makes sense. I guess I will have to hardcode the property names and just update it manually if there are any changes. Thank you!
– C.Programming
Mar 28 at 21:52
That's a bummer. But makes sense. I guess I will have to hardcode the property names and just update it manually if there are any changes. Thank you!
– C.Programming
Mar 28 at 21:52
That's a bummer. But makes sense. I guess I will have to hardcode the property names and just update it manually if there are any changes. Thank you!
– C.Programming
Mar 28 at 21:52
add a comment
|
From documentation I see the following for return value of Object.keys()
:
An array of strings that represent all the enumerable properties of the given object.
Source: Object.keys().
Which means for example if you have a class what you instantiate from JavaScript then you can get the keys into an array but not from the interface (from TypeScript):
let redApple = new Apple();
let keys = Object.keys(redApple);
let classKeys = Object.keys(Apple);
console.log('keys of the instance', keys);
console.log('keys of the class', classKeys);
function Apple()
this.id = '';
this.name = '';
this.status = '';
We should remember always that at the end TypeScript will be transpiled to JavaScript.
add a comment
|
From documentation I see the following for return value of Object.keys()
:
An array of strings that represent all the enumerable properties of the given object.
Source: Object.keys().
Which means for example if you have a class what you instantiate from JavaScript then you can get the keys into an array but not from the interface (from TypeScript):
let redApple = new Apple();
let keys = Object.keys(redApple);
let classKeys = Object.keys(Apple);
console.log('keys of the instance', keys);
console.log('keys of the class', classKeys);
function Apple()
this.id = '';
this.name = '';
this.status = '';
We should remember always that at the end TypeScript will be transpiled to JavaScript.
add a comment
|
From documentation I see the following for return value of Object.keys()
:
An array of strings that represent all the enumerable properties of the given object.
Source: Object.keys().
Which means for example if you have a class what you instantiate from JavaScript then you can get the keys into an array but not from the interface (from TypeScript):
let redApple = new Apple();
let keys = Object.keys(redApple);
let classKeys = Object.keys(Apple);
console.log('keys of the instance', keys);
console.log('keys of the class', classKeys);
function Apple()
this.id = '';
this.name = '';
this.status = '';
We should remember always that at the end TypeScript will be transpiled to JavaScript.
From documentation I see the following for return value of Object.keys()
:
An array of strings that represent all the enumerable properties of the given object.
Source: Object.keys().
Which means for example if you have a class what you instantiate from JavaScript then you can get the keys into an array but not from the interface (from TypeScript):
let redApple = new Apple();
let keys = Object.keys(redApple);
let classKeys = Object.keys(Apple);
console.log('keys of the instance', keys);
console.log('keys of the class', classKeys);
function Apple()
this.id = '';
this.name = '';
this.status = '';
We should remember always that at the end TypeScript will be transpiled to JavaScript.
let redApple = new Apple();
let keys = Object.keys(redApple);
let classKeys = Object.keys(Apple);
console.log('keys of the instance', keys);
console.log('keys of the class', classKeys);
function Apple()
this.id = '';
this.name = '';
this.status = '';
let redApple = new Apple();
let keys = Object.keys(redApple);
let classKeys = Object.keys(Apple);
console.log('keys of the instance', keys);
console.log('keys of the class', classKeys);
function Apple()
this.id = '';
this.name = '';
this.status = '';
edited Mar 28 at 21:58
answered Mar 28 at 21:50
norbitrialnorbitrial
1,2174 silver badges14 bronze badges
1,2174 silver badges14 bronze badges
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%2f55407235%2fobject-keys-for-a-non-instantiated-object-interface-type%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
2
As Sam Herrmann mentioned, it sounds impossible. Why do you want to do this?
– Dakota Jang
Mar 28 at 21:47
I wanted to set a constant at the top of my file that has all the properties of the interface. I iterate through them for the diffing tool I am writing, so I wanted to keep them dynamic in case any properties got added or deleted.
– C.Programming
Mar 28 at 21:52
Isn't that the reason why we use Typescript?
class Foo implements Bar
would complain ifFoo
is missing some implementations inBar
. If you want to prevent modification in runtime, you could tryObject.freeze
. Though I have a feeling that's not exactly something you are looking for.– Dakota Jang
Mar 31 at 2:08