-
Notifications
You must be signed in to change notification settings - Fork 235
narrow AnyType to StringType in mapped types #2412
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a73dac3
get basic case
srsudar e5fea9f
fix test
srsudar 31bec0e
add some tests
srsudar 098b4e7
better file
srsudar 79548ac
include test for template literal
srsudar 5ac6aaf
rm thing
srsudar ccab3b1
correct template literal
srsudar 0e8140f
Merge branch 'next' into sam-any-index-type
srsudar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| import { assertValidSchema } from "../../utils"; | ||
| import { test } from "node:test"; | ||
|
|
||
| test("type-mapped-any", assertValidSchema("type-mapped-any", "MyObject")); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export type MyObject = Record<any, string>; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| { | ||
| "$ref": "#/definitions/MyObject", | ||
| "$schema": "http://json-schema.org/draft-07/schema#", | ||
| "definitions": { | ||
| "MyObject": { | ||
| "additionalProperties": { | ||
| "type": "string" | ||
| }, | ||
| "type": "object" | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| import { assertValidSchema } from "../../utils"; | ||
| import { test } from "node:test"; | ||
|
|
||
| test("type-mapped-template-literal", assertValidSchema("type-mapped-template-literal", "MyObject")); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| type AorB = "A" | "B"; | ||
|
|
||
| export type MyObject = Record<`letter-${AorB}`, string>; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| { | ||
| "$ref": "#/definitions/MyObject", | ||
| "$schema": "http://json-schema.org/draft-07/schema#", | ||
| "definitions": { | ||
| "MyObject": { | ||
| "additionalProperties": false, | ||
| "properties": { | ||
| "letter-A": { | ||
| "type": "string" | ||
| }, | ||
| "letter-B": { | ||
| "type": "string" | ||
| } | ||
| }, | ||
| "required": [ | ||
| "letter-A", | ||
| "letter-B" | ||
| ], | ||
| "type": "object" | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we do the same for unknown? what are all valid ts.types to index an object?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah good point. With this code:
This is the error I get:
But afaik, everything gets coerced to a string anyways in TS. Eg:
So I would think that this perhaps could just never throw, and instead always default to
string? As-is, this it kind of seems like an additional, more restrictive typecheck.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't keys be symbols (in JS and TS) or string literals (in TS) as well? So it's not always string, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the full list according to the error message:
But I thought pretty much anything can be a key due to
toStringbehavior. eg:It looks like
Symboldoes make it into the object, but it doesn't surviveJSON.stringify(). Wow, TIL.I'm not sure what makes the most sense here. I'm pretty new to working with the TS AST like this. Happy to try whatever you think makes sense.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where did you try Symbol?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think start by making more tests cases with more types (ideally in the same test example rather than creating a ton of examples).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Symbolhere:By "ideally in the same test example rather than creating a ton of examples" do you mean just in a single
main.tsandschema.jsonfile?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I realized that you guys had already covered most of these, and in a pattern that I was ignoring.
numberis handled here:https://github.com/vega/ts-json-schema-generator/blob/next/test/valid-data/type-mapped-number/main.ts
symbolis handled here:https://github.com/vega/ts-json-schema-generator/blob/next/test/valid-data/type-mapped-symbol/main.ts
I copied this for
anyand for a template literal (which I think are the last two that are uncovered, based on this:I stuck both of these in their own files, to keep the pattern going. Lmk if you'd prefer them in a single file. I confirmed that
Record<any, string>is failing without this change.I feel like this shouldn't need to handle
unknown, since that isn't valid TS I don't think.