-
-
Notifications
You must be signed in to change notification settings - Fork 170
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix
typia.functional
module for async or implicit return type case.
When user inputs implicit return typed function into the `typia.functional` module, `typia.functional` could not validate exact return type value. Also, if the input parameter function is a typeof async, it could not be validated, either. This PR fixes such bugs.
- Loading branch information
Showing
20 changed files
with
168 additions
and
186 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -1,12 +1,13 @@ | ||
import fs from "fs"; | ||
import typia from "typia"; | ||
|
||
type Something = Promise<number>; | ||
const plus = async (x: number, y: number): Something => x + y; | ||
|
||
try { | ||
fs.mkdirSync(__dirname + "/../bin"); | ||
} catch {} | ||
const func = typia.functional.validateFunction( | ||
(x: number, y: number): number => x + y, | ||
); | ||
const func = typia.functional.isReturn(plus); | ||
fs.writeFileSync(`${__dirname}/../bin/functional.js`, func.toString(), "utf8"); | ||
|
||
console.log(func(3, 4)); |
This file contains 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 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 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 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 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 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 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 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 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 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 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 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
32 changes: 32 additions & 0 deletions
32
src/programmers/functional/internal/FunctionalGeneralProgrammer.ts
This file contains 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,32 @@ | ||
import ts from "typescript"; | ||
|
||
import { TypeFactory } from "../../../factories/TypeFactory"; | ||
|
||
export namespace FunctionalGeneralProgrammer { | ||
export interface IOutput { | ||
type: ts.Type; | ||
async: boolean; | ||
} | ||
export const getReturnType = | ||
(checker: ts.TypeChecker) => | ||
(declaration: ts.FunctionDeclaration): IOutput => { | ||
const signature: ts.Signature | undefined = | ||
checker.getSignatureFromDeclaration(declaration); | ||
const type: ts.Type = | ||
signature?.getReturnType() ?? | ||
checker.getTypeFromTypeNode(TypeFactory.keyword("any")); | ||
|
||
if (type.symbol?.name === "Promise") { | ||
const generic: readonly ts.Type[] = checker.getTypeArguments( | ||
type as ts.TypeReference, | ||
); | ||
return generic.length === 1 | ||
? { type: generic[0]!, async: true } | ||
: { | ||
type: checker.getTypeFromTypeNode(TypeFactory.keyword("any")), | ||
async: false, | ||
}; | ||
} | ||
return { type, async: false }; | ||
}; | ||
} |
This file contains 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
Oops, something went wrong.