mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-11 10:00:13 -06:00
Merge pull request #16497 from Microsoft/use-checker-for-decl-emit-of-optional-param-props
Use checker for declaration emit of optional, uninitialised parameter properties
This commit is contained in:
commit
9f69cd5a62
@ -23008,6 +23008,13 @@ namespace ts {
|
||||
!(getModifierFlags(parameter) & ModifierFlags.ParameterPropertyModifier);
|
||||
}
|
||||
|
||||
function isOptionalUninitializedParameterProperty(parameter: ParameterDeclaration) {
|
||||
return strictNullChecks &&
|
||||
isOptionalParameter(parameter) &&
|
||||
!parameter.initializer &&
|
||||
!!(getModifierFlags(parameter) & ModifierFlags.ParameterPropertyModifier);
|
||||
}
|
||||
|
||||
function getNodeCheckFlags(node: Node): NodeCheckFlags {
|
||||
return getNodeLinks(node).flags;
|
||||
}
|
||||
@ -23217,6 +23224,7 @@ namespace ts {
|
||||
isDeclarationVisible,
|
||||
isImplementationOfOverload,
|
||||
isRequiredInitializedParameter,
|
||||
isOptionalUninitializedParameterProperty,
|
||||
writeTypeOfDeclaration,
|
||||
writeReturnTypeOfSignatureDeclaration,
|
||||
writeTypeOfExpression,
|
||||
|
||||
@ -335,9 +335,12 @@ namespace ts {
|
||||
write(": ");
|
||||
|
||||
// use the checker's type, not the declared type,
|
||||
// for non-optional initialized parameters that aren't a parameter property
|
||||
// for optional parameter properties
|
||||
// and also for non-optional initialized parameters that aren't a parameter property
|
||||
// these types may need to add `undefined`.
|
||||
const shouldUseResolverType = declaration.kind === SyntaxKind.Parameter &&
|
||||
resolver.isRequiredInitializedParameter(declaration as ParameterDeclaration);
|
||||
(resolver.isRequiredInitializedParameter(declaration as ParameterDeclaration) ||
|
||||
resolver.isOptionalUninitializedParameterProperty(declaration as ParameterDeclaration));
|
||||
if (type && !shouldUseResolverType) {
|
||||
// Write the type
|
||||
emitType(type);
|
||||
|
||||
@ -2814,6 +2814,7 @@ namespace ts {
|
||||
collectLinkedAliases(node: Identifier): Node[];
|
||||
isImplementationOfOverload(node: FunctionLikeDeclaration): boolean | undefined;
|
||||
isRequiredInitializedParameter(node: ParameterDeclaration): boolean;
|
||||
isOptionalUninitializedParameterProperty(node: ParameterDeclaration): boolean;
|
||||
writeTypeOfDeclaration(declaration: AccessorDeclaration | VariableLikeDeclaration, enclosingDeclaration: Node, flags: TypeFormatFlags, writer: SymbolWriter): void;
|
||||
writeReturnTypeOfSignatureDeclaration(signatureDeclaration: SignatureDeclaration, enclosingDeclaration: Node, flags: TypeFormatFlags, writer: SymbolWriter): void;
|
||||
writeTypeOfExpression(expr: Expression, enclosingDeclaration: Node, flags: TypeFormatFlags, writer: SymbolWriter): void;
|
||||
|
||||
@ -0,0 +1,24 @@
|
||||
//// [declarationEmitParameterProperty.ts]
|
||||
export class Foo {
|
||||
constructor(public bar?: string) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//// [declarationEmitParameterProperty.js]
|
||||
"use strict";
|
||||
exports.__esModule = true;
|
||||
var Foo = (function () {
|
||||
function Foo(bar) {
|
||||
this.bar = bar;
|
||||
}
|
||||
return Foo;
|
||||
}());
|
||||
exports.Foo = Foo;
|
||||
|
||||
|
||||
//// [declarationEmitParameterProperty.d.ts]
|
||||
export declare class Foo {
|
||||
bar: string | undefined;
|
||||
constructor(bar?: string | undefined);
|
||||
}
|
||||
@ -0,0 +1,9 @@
|
||||
=== tests/cases/compiler/declarationEmitParameterProperty.ts ===
|
||||
export class Foo {
|
||||
>Foo : Symbol(Foo, Decl(declarationEmitParameterProperty.ts, 0, 0))
|
||||
|
||||
constructor(public bar?: string) {
|
||||
>bar : Symbol(Foo.bar, Decl(declarationEmitParameterProperty.ts, 1, 14))
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,9 @@
|
||||
=== tests/cases/compiler/declarationEmitParameterProperty.ts ===
|
||||
export class Foo {
|
||||
>Foo : Foo
|
||||
|
||||
constructor(public bar?: string) {
|
||||
>bar : string | undefined
|
||||
}
|
||||
}
|
||||
|
||||
@ -131,12 +131,12 @@ interface Foo {
|
||||
}
|
||||
declare function test1(x: Foo): void;
|
||||
declare class Bar {
|
||||
d: number;
|
||||
d: number | undefined;
|
||||
e: number;
|
||||
a: number;
|
||||
b?: number;
|
||||
c?: number | undefined;
|
||||
constructor(d?: number, e?: number);
|
||||
constructor(d?: number | undefined, e?: number);
|
||||
f(): number;
|
||||
g?(): number;
|
||||
h?(): number;
|
||||
|
||||
6
tests/cases/compiler/declarationEmitParameterProperty.ts
Normal file
6
tests/cases/compiler/declarationEmitParameterProperty.ts
Normal file
@ -0,0 +1,6 @@
|
||||
// @strictNullChecks: true
|
||||
// @declaration: true
|
||||
export class Foo {
|
||||
constructor(public bar?: string) {
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user