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

Optional constructor arguments with accessors are being added to .d.ts as optional class members #8788

Closed
dustinhorne opened this issue May 24, 2016 · 6 comments
Labels
Duplicate An existing issue was already created

Comments

@dustinhorne
Copy link

dustinhorne commented May 24, 2016

TypeScript Version:
nightly (1.9.0-beta-20160521-3)

When creating a class, if a parameter is added to the constructor as optional and given an accessor (such as private), the compiler generates it as an optional class member in the .d.ts file which then throws a syntax error.

Expected Behavior
Either field would be added to the class and set with the optional parameter, meaning it could be null or undefined or a syntax error is displayed at compile time.

Actual behavior:
Parameter is added as an optional class field resulting in an error.

Example class declaration:

export class Foo {
    constructor(private someValue?: any) { }
}

What The Compiler Produces in the Resulting .d.ts:

class Foo {
    private someValue?;
}

and the following error:
Error TS1112 A class member cannot be declared optional.

@RyanCavanaugh RyanCavanaugh added Bug A bug in TypeScript Needs More Info The issue still hasn't been fully clarified and removed Bug A bug in TypeScript labels May 24, 2016
@RyanCavanaugh
Copy link
Member

Optional class members are allowed now (#8625). Are you generating the file with one version of the compiler and consuming it with an older version? The provided generated snippet compiles without error in master.

@mhegazy mhegazy added Bug A bug in TypeScript and removed Bug A bug in TypeScript labels May 24, 2016
@dustinhorne
Copy link
Author

We are using it within Visual Studio so technically we're using tools version 1.8. Is there an updated version of the Visual Studio tooling? It compiles fine, just shows as an error in the Visual Studio Error List after compilation because it's in the generated .d.ts file.

@mhegazy
Copy link
Contributor

mhegazy commented May 24, 2016

you can setup VS to use the latest nightly builds, see https://github.com/Microsoft/TypeScript-Handbook/blob/master/pages/Nightly%20Builds.md

@dustinhorne
Copy link
Author

Thanks, I'll check it out. We're not currently using npm so we will just avoid the error for now and wait for the tooling installer.

@mhegazy
Copy link
Contributor

mhegazy commented May 24, 2016

i think we have to fix this issue anyways. #8795 provides a compelling example of why ? should not bind to the property.

@mhegazy
Copy link
Contributor

mhegazy commented May 24, 2016

closing in favor of #8795

@mhegazy mhegazy closed this as completed May 24, 2016
@mhegazy mhegazy added Duplicate An existing issue was already created and removed Needs More Info The issue still hasn't been fully clarified labels May 24, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants