Skip to content

Always emit any for get accessors #36279

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 23 additions & 6 deletions src/compiler/transformers/declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ namespace ts {
p.dotDotDotToken,
filterBindingPatternInitializers(p.name),
resolver.isOptionalParameter(p) ? (p.questionToken || createToken(SyntaxKind.QuestionToken)) : undefined,
ensureType(p, type || p.type, /*ignorePrivate*/ true), // Ignore private param props, since this type is going straight back into a param
ensureType(p, type || p.type, TypeElisionStrategyForPrivates.PreserveOriginal), // preserve types from private parameter properties, since the type is still needed for the parameter side
ensureNoInitializer(p)
);
if (!suppressNewDiagnosticContexts) {
Expand Down Expand Up @@ -476,10 +476,24 @@ namespace ts {
| PropertyDeclaration
| PropertySignature;

function ensureType(node: HasInferredType, type: TypeNode | undefined, ignorePrivate?: boolean): TypeNode | undefined {
if (!ignorePrivate && hasModifier(node, ModifierFlags.Private)) {
// Private nodes emit no types (except private parameter properties, whose parameter types are actually visible)
return;
const enum TypeElisionStrategyForPrivates {
/** Keep the original type declaration around */
PreserveOriginal,
/** Drop types if its containing declaration is `private`. */
Drop,
/** Explicitly declare the type with `any`. */
ExplicitAny,
}

function ensureType(node: HasInferredType, type: TypeNode | undefined, privateStrategy = TypeElisionStrategyForPrivates.Drop): TypeNode | undefined {
if (privateStrategy !== TypeElisionStrategyForPrivates.PreserveOriginal && hasModifier(node, ModifierFlags.Private)) {
switch (privateStrategy) {
case TypeElisionStrategyForPrivates.Drop:
// Private nodes emit no types (except private parameter properties, whose parameter types are actually visible)
return;
case TypeElisionStrategyForPrivates.ExplicitAny:
return createKeywordTypeNode(SyntaxKind.AnyKeyword);
}
}
if (shouldPrintWithInitializer(node)) {
// Literal const declarations will have an initializer ensured rather than a type
Expand Down Expand Up @@ -876,13 +890,16 @@ namespace ts {
return cleanup(/*returnValue*/ undefined);
}
const accessorType = getTypeAnnotationFromAllAccessorDeclarations(input, resolver.getAllAccessorDeclarations(input));
// We have to make sure that ignorePrivate is set because TypeScript 3.6.0 to 3.6.4 thinks it's an implicit any error.
const ensuredType = ensureType(input, accessorType, TypeElisionStrategyForPrivates.ExplicitAny);

return cleanup(updateGetAccessor(
input,
/*decorators*/ undefined,
ensureModifiers(input),
input.name,
updateAccessorParamsList(input, hasModifier(input, ModifierFlags.Private)),
ensureType(input, accessorType),
ensuredType,
/*body*/ undefined));
}
case SyntaxKind.SetAccessor: {
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/ambientAccessors(target=es3).js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ declare class C {
declare class C {
static get a(): string;
static set a(value: string);
private static get b();
private static get b(): any;
private static set b(value);
get x(): string;
set x(value: string);
private get y();
private get y(): any;
private set y(value);
}
4 changes: 2 additions & 2 deletions tests/baselines/reference/ambientAccessors(target=es5).js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ declare class C {
declare class C {
static get a(): string;
static set a(value: string);
private static get b();
private static get b(): any;
private static set b(value);
get x(): string;
set x(value: string);
private get y();
private get y(): any;
private set y(value);
}
2 changes: 1 addition & 1 deletion tests/baselines/reference/classdecl.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ declare class a {
y: number;
};
private static d2;
private static get p3();
private static get p3(): any;
private pv3;
private foo;
}
Expand Down
8 changes: 4 additions & 4 deletions tests/baselines/reference/commentsClassMembers.js
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ declare class c1 {
/** sum with property*/
private pp2;
/** getter property*/
private get pp3();
private get pp3(): any;
/** setter property*/
private set pp3(value);
/** Constructor method*/
Expand All @@ -513,7 +513,7 @@ declare class c1 {
set nc_p3(value: number);
private nc_pp1;
private nc_pp2;
private get nc_pp3();
private get nc_pp3(): any;
private set nc_pp3(value);
static nc_s1: number;
static nc_s2(b: number): number;
Expand All @@ -525,7 +525,7 @@ declare class c1 {
set a_p3(value: number);
private a_pp1;
private a_pp2;
private get a_pp3();
private get a_pp3(): any;
private set a_pp3(value);
static a_s1: number;
static a_s2(b: number): number;
Expand All @@ -544,7 +544,7 @@ declare class c1 {
/** sum with property */
private b_pp2;
/** getter property */
private get b_pp3();
private get b_pp3(): any;
/** setter property */
private set b_pp3(value);
/** s1 is static property of c1 */
Expand Down
8 changes: 4 additions & 4 deletions tests/baselines/reference/declFileAccessors.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ export declare class c1 {
/** setter property*/
set p3(/** this is value*/ value: number);
/** private getter property*/
private get pp3();
private get pp3(): any;
/** private setter property*/
private set pp3(value);
/** static getter property*/
Expand All @@ -286,7 +286,7 @@ export declare class c1 {
static set s3(/** this is value*/ value: number);
get nc_p3(): number;
set nc_p3(value: number);
private get nc_pp3();
private get nc_pp3(): any;
private set nc_pp3(value);
static get nc_s3(): string;
static set nc_s3(value: string);
Expand All @@ -301,7 +301,7 @@ declare class c2 {
/** setter property*/
set p3(/** this is value*/ value: number);
/** private getter property*/
private get pp3();
private get pp3(): any;
/** private setter property*/
private set pp3(value);
/** static getter property*/
Expand All @@ -310,7 +310,7 @@ declare class c2 {
static set s3(/** this is value*/ value: number);
get nc_p3(): number;
set nc_p3(value: number);
private get nc_pp3();
private get nc_pp3(): any;
private set nc_pp3(value);
static get nc_s3(): string;
static set nc_s3(value: string);
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/declFilePrivateStatic.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ declare class C {
static y: number;
private static a;
static b(): void;
private static get c();
private static get c(): any;
static get d(): number;
private static set e(value);
static set f(v: any);
Expand Down
8 changes: 4 additions & 4 deletions tests/baselines/reference/giant.js
Original file line number Diff line number Diff line change
Expand Up @@ -1124,7 +1124,7 @@ export declare class eC {
psF(param: any): void;
set psF(param: any);
private rgF;
private get rgF();
private get rgF(): any;
private rsF;
private set rsF(value);
static tV: any;
Expand Down Expand Up @@ -1176,7 +1176,7 @@ export declare module eM {
psF(param: any): void;
set psF(param: any);
private rgF;
private get rgF();
private get rgF(): any;
private rsF;
private set rsF(value);
static tV: any;
Expand Down Expand Up @@ -1241,7 +1241,7 @@ export declare module eM {
psF(param: any): void;
set psF(param: any);
private rgF;
private get rgF();
private get rgF(): any;
private rsF;
private set rsF(value);
static tV: any;
Expand Down Expand Up @@ -1281,7 +1281,7 @@ export declare class eaC {
psF(param: any): void;
set psF(param: any);
private rgF;
private get rgF();
private get rgF(): any;
private rsF;
private set rsF(value);
static tV: any;
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/moduledecl.js
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ declare module exportTests {
class C3_public {
private getC2_private;
private setC2_private;
private get c2();
private get c2(): any;
getC1_public(): C1_public;
setC1_public(arg: C1_public): void;
get c1(): C1_public;
Expand Down
88 changes: 44 additions & 44 deletions tests/baselines/reference/privacyAccessorDeclFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -3563,23 +3563,23 @@ export declare class publicClass {
}
export declare class publicClassWithWithPrivateGetAccessorTypes {
static get myPublicStaticMethod(): privateClass;
private static get myPrivateStaticMethod();
private static get myPrivateStaticMethod(): any;
get myPublicMethod(): privateClass;
private get myPrivateMethod();
private get myPrivateMethod(): any;
static get myPublicStaticMethod1(): privateClass;
private static get myPrivateStaticMethod1();
private static get myPrivateStaticMethod1(): any;
get myPublicMethod1(): privateClass;
private get myPrivateMethod1();
private get myPrivateMethod1(): any;
}
export declare class publicClassWithWithPublicGetAccessorTypes {
static get myPublicStaticMethod(): publicClass;
private static get myPrivateStaticMethod();
private static get myPrivateStaticMethod(): any;
get myPublicMethod(): publicClass;
private get myPrivateMethod();
private get myPrivateMethod(): any;
static get myPublicStaticMethod1(): publicClass;
private static get myPrivateStaticMethod1();
private static get myPrivateStaticMethod1(): any;
get myPublicMethod1(): publicClass;
private get myPrivateMethod1();
private get myPrivateMethod1(): any;
}
export declare class publicClassWithWithPrivateSetAccessorTypes {
static set myPublicStaticMethod(param: privateClass);
Expand Down Expand Up @@ -3610,23 +3610,23 @@ export declare module publicModule {
}
export class publicClassWithWithPrivateGetAccessorTypes {
static get myPublicStaticMethod(): privateClass;
private static get myPrivateStaticMethod();
private static get myPrivateStaticMethod(): any;
get myPublicMethod(): privateClass;
private get myPrivateMethod();
private get myPrivateMethod(): any;
static get myPublicStaticMethod1(): privateClass;
private static get myPrivateStaticMethod1();
private static get myPrivateStaticMethod1(): any;
get myPublicMethod1(): privateClass;
private get myPrivateMethod1();
private get myPrivateMethod1(): any;
}
export class publicClassWithWithPublicGetAccessorTypes {
static get myPublicStaticMethod(): publicClass;
private static get myPrivateStaticMethod();
private static get myPrivateStaticMethod(): any;
get myPublicMethod(): publicClass;
private get myPrivateMethod();
private get myPrivateMethod(): any;
static get myPublicStaticMethod1(): publicClass;
private static get myPrivateStaticMethod1();
private static get myPrivateStaticMethod1(): any;
get myPublicMethod1(): publicClass;
private get myPrivateMethod1();
private get myPrivateMethod1(): any;
}
export class publicClassWithWithPrivateSetAccessorTypes {
static set myPublicStaticMethod(param: privateClass);
Expand Down Expand Up @@ -3659,23 +3659,23 @@ declare module privateModule {
}
export class publicClassWithWithPrivateGetAccessorTypes {
static get myPublicStaticMethod(): privateClass;
private static get myPrivateStaticMethod();
private static get myPrivateStaticMethod(): any;
get myPublicMethod(): privateClass;
private get myPrivateMethod();
private get myPrivateMethod(): any;
static get myPublicStaticMethod1(): privateClass;
private static get myPrivateStaticMethod1();
private static get myPrivateStaticMethod1(): any;
get myPublicMethod1(): privateClass;
private get myPrivateMethod1();
private get myPrivateMethod1(): any;
}
export class publicClassWithWithPublicGetAccessorTypes {
static get myPublicStaticMethod(): publicClass;
private static get myPrivateStaticMethod();
private static get myPrivateStaticMethod(): any;
get myPublicMethod(): publicClass;
private get myPrivateMethod();
private get myPrivateMethod(): any;
static get myPublicStaticMethod1(): publicClass;
private static get myPrivateStaticMethod1();
private static get myPrivateStaticMethod1(): any;
get myPublicMethod1(): publicClass;
private get myPrivateMethod1();
private get myPrivateMethod1(): any;
}
export class publicClassWithWithPrivateSetAccessorTypes {
static set myPublicStaticMethod(param: privateClass);
Expand Down Expand Up @@ -3707,13 +3707,13 @@ declare class publicClassInGlobal {
}
declare class publicClassInGlobalWithPublicGetAccessorTypes {
static get myPublicStaticMethod(): publicClassInGlobal;
private static get myPrivateStaticMethod();
private static get myPrivateStaticMethod(): any;
get myPublicMethod(): publicClassInGlobal;
private get myPrivateMethod();
private get myPrivateMethod(): any;
static get myPublicStaticMethod1(): publicClassInGlobal;
private static get myPrivateStaticMethod1();
private static get myPrivateStaticMethod1(): any;
get myPublicMethod1(): publicClassInGlobal;
private get myPrivateMethod1();
private get myPrivateMethod1(): any;
}
declare class publicClassInGlobalWithWithPublicSetAccessorTypes {
static set myPublicStaticMethod(param: publicClassInGlobal);
Expand All @@ -3733,23 +3733,23 @@ declare module publicModuleInGlobal {
}
export class publicClassWithWithPrivateGetAccessorTypes {
static get myPublicStaticMethod(): privateClass;
private static get myPrivateStaticMethod();
private static get myPrivateStaticMethod(): any;
get myPublicMethod(): privateClass;
private get myPrivateMethod();
private get myPrivateMethod(): any;
static get myPublicStaticMethod1(): privateClass;
private static get myPrivateStaticMethod1();
private static get myPrivateStaticMethod1(): any;
get myPublicMethod1(): privateClass;
private get myPrivateMethod1();
private get myPrivateMethod1(): any;
}
export class publicClassWithWithPublicGetAccessorTypes {
static get myPublicStaticMethod(): publicClass;
private static get myPrivateStaticMethod();
private static get myPrivateStaticMethod(): any;
get myPublicMethod(): publicClass;
private get myPrivateMethod();
private get myPrivateMethod(): any;
static get myPublicStaticMethod1(): publicClass;
private static get myPrivateStaticMethod1();
private static get myPrivateStaticMethod1(): any;
get myPublicMethod1(): publicClass;
private get myPrivateMethod1();
private get myPrivateMethod1(): any;
}
export class publicClassWithWithPrivateSetAccessorTypes {
static set myPublicStaticMethod(param: privateClass);
Expand Down Expand Up @@ -3777,23 +3777,23 @@ declare module publicModuleInGlobal {
}
export class publicClassWithWithPrivateGetAccessorTypes {
static get myPublicStaticMethod(): privateClass;
private static get myPrivateStaticMethod();
private static get myPrivateStaticMethod(): any;
get myPublicMethod(): privateClass;
private get myPrivateMethod();
private get myPrivateMethod(): any;
static get myPublicStaticMethod1(): privateClass;
private static get myPrivateStaticMethod1();
private static get myPrivateStaticMethod1(): any;
get myPublicMethod1(): privateClass;
private get myPrivateMethod1();
private get myPrivateMethod1(): any;
}
export class publicClassWithWithPublicGetAccessorTypes {
static get myPublicStaticMethod(): publicClass;
private static get myPrivateStaticMethod();
private static get myPrivateStaticMethod(): any;
get myPublicMethod(): publicClass;
private get myPrivateMethod();
private get myPrivateMethod(): any;
static get myPublicStaticMethod1(): publicClass;
private static get myPrivateStaticMethod1();
private static get myPrivateStaticMethod1(): any;
get myPublicMethod1(): publicClass;
private get myPrivateMethod1();
private get myPrivateMethod1(): any;
}
export class publicClassWithWithPrivateSetAccessorTypes {
static set myPublicStaticMethod(param: privateClass);
Expand Down
Loading