Skip to content
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

Set explicit void return type in function builder if set by the user #24

Open
Legion2 opened this issue Jul 24, 2023 · 5 comments · May be fixed by #25
Open

Set explicit void return type in function builder if set by the user #24

Legion2 opened this issue Jul 24, 2023 · 5 comments · May be fixed by #25

Comments

@Legion2
Copy link

Legion2 commented Jul 24, 2023

When the return type is explicitly set to void it should also be set in the generated code explicitly to void. When enabling strict type checking in typescript compiler with the noImplicitAny option, all functions without explicit return type default to any.
Following code must be changed:

if (returnType != null && returnType != TypeName.VOID) {
codeWriter.emitCode(CodeBlock.of(": %T", returnType))
}

@kdubb
Copy link
Contributor

kdubb commented Jul 25, 2023

Agreed.

@Legion2 Legion2 linked a pull request Jul 25, 2023 that will close this issue
@Legion2
Copy link
Author

Legion2 commented Jul 25, 2023

I created #25

@tinacious
Copy link

I agree it would be great to add this to prevent 'functionName', which lacks return-type annotation, implicitly has an 'any' return type.

It's also important to note that noImplicitAny is the default configuration when using strict mode:

image

In case anyone finding this is interested in a workaround until the issue is addressed, I defined a type alias Void and used that as the value passed to FunctionSpec#returns(). It's kinda gross but it works. 🤷🏻‍♀️ Another option is to add TypeScript and ESLint disabling comments but that would just clear the error rather than enforce a void return type, so I opted for Void:

// Workaround for https://github.com/outfoxx/typescriptpoet/issues/24
val voidType = TypeAliasSpec
    .builder("Void", TypeName.VOID)
    .addTSDoc("Workaround for https://github.com/outfoxx/typescriptpoet/issues/24")
    .build()
fileSpecBuilder.addType(voidType)

val someInterface = InterfaceSpec
    .builder("MyInterface")
    .addFunction(
        FunctionSpec
            .builder("doSomething")
            .addModifiers(Modifier.ABSTRACT)
            .returns(TypeName.implicit("Void"))
            .build()
    )
    .build()
fileSpecBuilder.addType(someInterface)

Which outputs:

/**
 * Workaround for https://github.com/outfoxx/typescriptpoet/issues/24 */
type Void = void;

interface MyInterface {

  doSomething(): Void;

}

@kdubb
Copy link
Contributor

kdubb commented Dec 31, 2024

@Legion2 @tinacious I've been quite preoccupied with other projects. I'll spend some time updating this project.

@kdubb
Copy link
Contributor

kdubb commented Dec 31, 2024

That includes merging #25 😉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants