Creating a type which defines keys of an object containing a specific nested propertyTypeScript: Creating an empty typed container arrayDefine objects to only contain elements of specific typeAngular2 object property typed as “number” changes to stringType for extracting nested object keysTypeScript type with properties of an object containing a specific typeHow to describe type that ensures specific property on nested objectsDefine in typescript object type to contain a specific property keyDefine a type for all possible object property typesTypescript error, how to correct this: Element implicitly has an 'any' type because typeTyping a record of a known type
What is the difference between nullifying your vote and not going to vote at all?
If Sweden was to magically float away, at what altitude would it be visible from the southern hemisphere?
If a massive object like Jupiter flew past the Earth how close would it need to come to pull people off of the surface?
Is it possible to change original filename of an exe?
Improve OR inside INNER JOIN
Asking bank to reduce APR instead of increasing credit limit
Is there an explanation for Austria's Freedom Party virtually retaining its vote share despite recent scandal?
Term for checking piece whose opponent daren't capture it
Draw a checker pattern with a black X in the center
How do I subvert the tropes of a train heist?
Is the world in Game of Thrones spherical or flat?
Lunar orbital rendezvous
Smart people send dumb people to a new planet on a space craft that crashes into a body of water
Future enhancements for the finite element method
Are UK pensions taxed twice?
Can an old DSLR be upgraded to match modern smartphone image quality
Is floating in space similar to falling under gravity?
What are the slash markings on Gatwick's 08R/26L?
When a current flow in an inductor is interrupted, what limits the voltage rise?
Expenditure in Poland - Forex doesn't have Zloty
Why does the UK have more political parties than the US?
find the Integer value after a string from a file
The deliberate use of misleading terminology
How to prevent bad sectors?
Creating a type which defines keys of an object containing a specific nested property
TypeScript: Creating an empty typed container arrayDefine objects to only contain elements of specific typeAngular2 object property typed as “number” changes to stringType for extracting nested object keysTypeScript type with properties of an object containing a specific typeHow to describe type that ensures specific property on nested objectsDefine in typescript object type to contain a specific property keyDefine a type for all possible object property typesTypescript error, how to correct this: Element implicitly has an 'any' type because typeTyping a record of a known type
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I have an interesting case where it would be very useful to dynamically define types from a central JSON data store. Let me explain.
I have a json file that contains a list of brands
// brands.json
"Airbus":
"keywords": ["A320", "A380"],
"type": ["planes"]
,
"Jaguar":
"keywords": ["fiesta"],
"origin": "UK",
"type": ["cars", "glasses"]
,
"Nissan":
"keywords": ["qashqai"],
"type": ["cars"]
Then I have my type definitions:
import brands from "./brands.json
type BrandNames= keyof typeof brands ;
type Brands =
[P in BrandNames]:
keywords: string[];
origin?: string;
type: string[];
;
I have created a type CompanieNames which is automatically generated and is equal to "Airbus" | "Jaguar" | "Nissan"
.
So far so good...
Now, I want to create a type CarBrands, which will be equal to "Jaguar" | "Nissan"
.
Doing type CarBrands = Exclude<CompanieNames, "Airbus">;
would work, but this is not dynamic.
So instead, I need to filter out all keys from Brands
which have a nested type
property that doesn't contain the string "cars"
.
Is it possible to do that?
typescript
add a comment |
I have an interesting case where it would be very useful to dynamically define types from a central JSON data store. Let me explain.
I have a json file that contains a list of brands
// brands.json
"Airbus":
"keywords": ["A320", "A380"],
"type": ["planes"]
,
"Jaguar":
"keywords": ["fiesta"],
"origin": "UK",
"type": ["cars", "glasses"]
,
"Nissan":
"keywords": ["qashqai"],
"type": ["cars"]
Then I have my type definitions:
import brands from "./brands.json
type BrandNames= keyof typeof brands ;
type Brands =
[P in BrandNames]:
keywords: string[];
origin?: string;
type: string[];
;
I have created a type CompanieNames which is automatically generated and is equal to "Airbus" | "Jaguar" | "Nissan"
.
So far so good...
Now, I want to create a type CarBrands, which will be equal to "Jaguar" | "Nissan"
.
Doing type CarBrands = Exclude<CompanieNames, "Airbus">;
would work, but this is not dynamic.
So instead, I need to filter out all keys from Brands
which have a nested type
property that doesn't contain the string "cars"
.
Is it possible to do that?
typescript
add a comment |
I have an interesting case where it would be very useful to dynamically define types from a central JSON data store. Let me explain.
I have a json file that contains a list of brands
// brands.json
"Airbus":
"keywords": ["A320", "A380"],
"type": ["planes"]
,
"Jaguar":
"keywords": ["fiesta"],
"origin": "UK",
"type": ["cars", "glasses"]
,
"Nissan":
"keywords": ["qashqai"],
"type": ["cars"]
Then I have my type definitions:
import brands from "./brands.json
type BrandNames= keyof typeof brands ;
type Brands =
[P in BrandNames]:
keywords: string[];
origin?: string;
type: string[];
;
I have created a type CompanieNames which is automatically generated and is equal to "Airbus" | "Jaguar" | "Nissan"
.
So far so good...
Now, I want to create a type CarBrands, which will be equal to "Jaguar" | "Nissan"
.
Doing type CarBrands = Exclude<CompanieNames, "Airbus">;
would work, but this is not dynamic.
So instead, I need to filter out all keys from Brands
which have a nested type
property that doesn't contain the string "cars"
.
Is it possible to do that?
typescript
I have an interesting case where it would be very useful to dynamically define types from a central JSON data store. Let me explain.
I have a json file that contains a list of brands
// brands.json
"Airbus":
"keywords": ["A320", "A380"],
"type": ["planes"]
,
"Jaguar":
"keywords": ["fiesta"],
"origin": "UK",
"type": ["cars", "glasses"]
,
"Nissan":
"keywords": ["qashqai"],
"type": ["cars"]
Then I have my type definitions:
import brands from "./brands.json
type BrandNames= keyof typeof brands ;
type Brands =
[P in BrandNames]:
keywords: string[];
origin?: string;
type: string[];
;
I have created a type CompanieNames which is automatically generated and is equal to "Airbus" | "Jaguar" | "Nissan"
.
So far so good...
Now, I want to create a type CarBrands, which will be equal to "Jaguar" | "Nissan"
.
Doing type CarBrands = Exclude<CompanieNames, "Airbus">;
would work, but this is not dynamic.
So instead, I need to filter out all keys from Brands
which have a nested type
property that doesn't contain the string "cars"
.
Is it possible to do that?
typescript
typescript
edited Mar 24 at 10:55
h-Reser
asked Mar 24 at 10:02
h-Reserh-Reser
377
377
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
If the types of the string literal types were preserved, we could do what you want (ie extract only the brands that have car
in the types
field). Typescript will however widen the types of the values in the types
array to string
so this information is lost to us when we actually look at the type of the json object.
Just for fun, using the as const
in 3.4 (unreleased yet) to preserve all string literal types, this is what the solution would look like:
let data =
"Airbus":
"keywords": ["A320", "A380"],
"type": ["planes"]
,
"Jaguar":
"keywords": ["fiesta"],
"origin": "UK",
"type": ["cars", "glasses"]
,
"Nissan":
"keywords": ["qashqai"],
"type": ["cars"]
as const;
type Data = typeof data;
type CarBrands =
[P in keyof Data]: 'cars' extends Data[P]['type'][number] ? P : never
[keyof Data]
Again the above does not work for imported json modules because typescript will not preserve the literal types in the string arrays and there is no way to tell it to do so at the time of writing.
I have the possibility to convert my json file to a typescript file in this case. And since version 3.4 is only a couple days away, your proposition is actually very helpful! Thanks
– h-Reser
Mar 24 at 15:13
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%2f55322614%2fcreating-a-type-which-defines-keys-of-an-object-containing-a-specific-nested-pro%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
If the types of the string literal types were preserved, we could do what you want (ie extract only the brands that have car
in the types
field). Typescript will however widen the types of the values in the types
array to string
so this information is lost to us when we actually look at the type of the json object.
Just for fun, using the as const
in 3.4 (unreleased yet) to preserve all string literal types, this is what the solution would look like:
let data =
"Airbus":
"keywords": ["A320", "A380"],
"type": ["planes"]
,
"Jaguar":
"keywords": ["fiesta"],
"origin": "UK",
"type": ["cars", "glasses"]
,
"Nissan":
"keywords": ["qashqai"],
"type": ["cars"]
as const;
type Data = typeof data;
type CarBrands =
[P in keyof Data]: 'cars' extends Data[P]['type'][number] ? P : never
[keyof Data]
Again the above does not work for imported json modules because typescript will not preserve the literal types in the string arrays and there is no way to tell it to do so at the time of writing.
I have the possibility to convert my json file to a typescript file in this case. And since version 3.4 is only a couple days away, your proposition is actually very helpful! Thanks
– h-Reser
Mar 24 at 15:13
add a comment |
If the types of the string literal types were preserved, we could do what you want (ie extract only the brands that have car
in the types
field). Typescript will however widen the types of the values in the types
array to string
so this information is lost to us when we actually look at the type of the json object.
Just for fun, using the as const
in 3.4 (unreleased yet) to preserve all string literal types, this is what the solution would look like:
let data =
"Airbus":
"keywords": ["A320", "A380"],
"type": ["planes"]
,
"Jaguar":
"keywords": ["fiesta"],
"origin": "UK",
"type": ["cars", "glasses"]
,
"Nissan":
"keywords": ["qashqai"],
"type": ["cars"]
as const;
type Data = typeof data;
type CarBrands =
[P in keyof Data]: 'cars' extends Data[P]['type'][number] ? P : never
[keyof Data]
Again the above does not work for imported json modules because typescript will not preserve the literal types in the string arrays and there is no way to tell it to do so at the time of writing.
I have the possibility to convert my json file to a typescript file in this case. And since version 3.4 is only a couple days away, your proposition is actually very helpful! Thanks
– h-Reser
Mar 24 at 15:13
add a comment |
If the types of the string literal types were preserved, we could do what you want (ie extract only the brands that have car
in the types
field). Typescript will however widen the types of the values in the types
array to string
so this information is lost to us when we actually look at the type of the json object.
Just for fun, using the as const
in 3.4 (unreleased yet) to preserve all string literal types, this is what the solution would look like:
let data =
"Airbus":
"keywords": ["A320", "A380"],
"type": ["planes"]
,
"Jaguar":
"keywords": ["fiesta"],
"origin": "UK",
"type": ["cars", "glasses"]
,
"Nissan":
"keywords": ["qashqai"],
"type": ["cars"]
as const;
type Data = typeof data;
type CarBrands =
[P in keyof Data]: 'cars' extends Data[P]['type'][number] ? P : never
[keyof Data]
Again the above does not work for imported json modules because typescript will not preserve the literal types in the string arrays and there is no way to tell it to do so at the time of writing.
If the types of the string literal types were preserved, we could do what you want (ie extract only the brands that have car
in the types
field). Typescript will however widen the types of the values in the types
array to string
so this information is lost to us when we actually look at the type of the json object.
Just for fun, using the as const
in 3.4 (unreleased yet) to preserve all string literal types, this is what the solution would look like:
let data =
"Airbus":
"keywords": ["A320", "A380"],
"type": ["planes"]
,
"Jaguar":
"keywords": ["fiesta"],
"origin": "UK",
"type": ["cars", "glasses"]
,
"Nissan":
"keywords": ["qashqai"],
"type": ["cars"]
as const;
type Data = typeof data;
type CarBrands =
[P in keyof Data]: 'cars' extends Data[P]['type'][number] ? P : never
[keyof Data]
Again the above does not work for imported json modules because typescript will not preserve the literal types in the string arrays and there is no way to tell it to do so at the time of writing.
answered Mar 24 at 13:59
Titian Cernicova-DragomirTitian Cernicova-Dragomir
80.9k46177
80.9k46177
I have the possibility to convert my json file to a typescript file in this case. And since version 3.4 is only a couple days away, your proposition is actually very helpful! Thanks
– h-Reser
Mar 24 at 15:13
add a comment |
I have the possibility to convert my json file to a typescript file in this case. And since version 3.4 is only a couple days away, your proposition is actually very helpful! Thanks
– h-Reser
Mar 24 at 15:13
I have the possibility to convert my json file to a typescript file in this case. And since version 3.4 is only a couple days away, your proposition is actually very helpful! Thanks
– h-Reser
Mar 24 at 15:13
I have the possibility to convert my json file to a typescript file in this case. And since version 3.4 is only a couple days away, your proposition is actually very helpful! Thanks
– h-Reser
Mar 24 at 15:13
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%2f55322614%2fcreating-a-type-which-defines-keys-of-an-object-containing-a-specific-nested-pro%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