-
Notifications
You must be signed in to change notification settings - Fork 193
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
Support tuple type narrowing, fix call expression parser #522
Conversation
Unfortunately, I have no more time to resolve these issues:
|
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.
Thank you! Let's remove the third party tests and boil down the problem to a unit test.
package.json
Outdated
@@ -51,6 +55,8 @@ | |||
"eslint": "^7.8.1", | |||
"eslint-config-prettier": "^6.11.0", | |||
"eslint-plugin-prettier": "^3.1.4", | |||
"fp-ts": "^2.8.2", |
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.
Let's not rely on specific third-party libraries for the tests and instead boil the test case down to a unit test.
@@ -17,7 +16,7 @@ export class ArrayLiteralExpressionNodeParser implements SubNodeParser { | |||
if (node.elements) { | |||
const elements = node.elements.map((t) => this.childNodeParser.createType(t, context)).filter(notUndefined); | |||
|
|||
return new ArrayType(new UnionType(elements)); | |||
return new TupleType(elements); |
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.
Nice!
test/valid-data-third-party.test.ts
Outdated
@@ -0,0 +1,5 @@ | |||
import { assertValidSchema } from "./utils"; |
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.
Let's remove this test.
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.
Awesome. Let's just fix a few more things and then I will merge the pull request and make a release.
@@ -14,10 +11,19 @@ export class CallExpressionParser implements SubNodeParser { | |||
} | |||
public createType(node: ts.CallExpression, context: Context): BaseType { |
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 you add a test that covers this code below?
return new TupleType([ | ||
new UnionType((type as any).typeArguments[0].types.map((t: any) => new LiteralType(t.value))), | ||
]); | ||
node.arguments.forEach((arg) => { |
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.
Please use for (const arg of node.arguments) {}
return node.kind === ts.SyntaxKind.PropertyAccessExpression; | ||
} | ||
|
||
public createType(node: ts.PropertyAccessExpression, context: Context): BaseType | undefined { |
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.
Please add a test that covers this case
@mooyoul These changes are great and I would love to merge them. Can you finish up the two small changes? |
Hmm, unfortunately, this change breaks const IDX = {
foo: 1,
bar: 1,
};
export const keys = Object.keys as <T>(o: T) => Extract<keyof T, string>[];
export const Foo = keys(IDX);
export type MyType = typeof Foo[number];
|
@mooyoul it would be awesome if you could help fix this case above. |
Changes
Add PropertyAccessExpression support (w/ unit test)
Supports schema generation of below code:
Support type Narrowing of tuple type using array-like object condition
Supports schema generation of below code:
Use TupleType in ArrayLiteralExpression parser by default
Fix broken CallExpressionParser
Resolves issue with 3rd party type imports (e.g. io-ts)
Add integration test with 3rd party io-ts package
Related Issues