TypeScript版本:
每晚(1.9.0-dev.20160217)
代码
export interface QueryMetadataFactory {
(selector: Type | string, {descendants, read}?: {
descendants?: boolean;
read?: any;
}): ParameterDecorator;
new (selector: Type | string, {descendants, read}?: {
descendants?: boolean;
read?: any;
}): QueryMetadata;
}
预期行为:
当使用严格的null检查进行编译时,我希望它能够编译,因为解构变量已被标记为可选,因为这可行:
export interface QueryMetadataFactory {
(selector: Type | string, whatever?: {
descendants?: boolean;
read?: any;
}): ParameterDecorator;
new (selector: Type | string, whatever?: {
descendants?: boolean;
read?: any;
}): QueryMetadata;
}
实际行为:
编译后:
node_modules/@angular/core/src/metadata.d.ts(356,32): error TS2459: Type '{ descendants?: boolean | undefined; read?: any; } | undefined' has no property 'descendants' and no string index signature.
node_modules/@angular/core/src/metadata.d.ts(356,45): error TS2459: Type '{ descendants?: boolean | undefined; read?: any; } | undefined' has no property 'read' and no string index signature.
node_modules/@angular/core/src/metadata.d.ts(360,36): error TS2459: Type '{ descendants?: boolean | undefined; read?: any; } | undefined' has no property 'descendants' and no string index signature.
node_modules/@angular/core/src/metadata.d.ts(360,49): error TS2459: Type '{ descendants?: boolean | undefined; read?: any; } | undefined' has no property 'read' and no string index signatu
这是临界点。 功能
function foo({ a, b }?: { a: number, b: number }) {
}
实际上是一个错误,因为您要破坏的参数可能是undefined
。 因此,无法实现示例中的确切签名。 现在,如果您将声明更改为
function foo({ a, b }: { a: number, b: number } = { a: 0, b: 0 }) {
}
它不再是错误,因为undefined
将被替换为默认值。 但是,从调用者的角度来看,该参数仍然是可选的,如果您现在生成一个声明文件,我们会吐出该声明
declare function foo({ a, b }?: { a: number, b: number }): void;
这会导致错误。 那是个错误。
顺便说一句,我建议避免在非实现签名中破坏参数。 它们实际上是一个实现细节,即函数实现是对参数进行了结构分解还是仅使用属性访问,都不应该真正在乎调用者。 但是,在声明文件的情况下,因为我们没有要发出的实际参数名称,所以我们(不情愿地)确实发出了破坏模式,因此我们需要修复该错误。
最有用的评论
这是临界点。 功能
实际上是一个错误,因为您要破坏的参数可能是
undefined
。 因此,无法实现示例中的确切签名。 现在,如果您将声明更改为它不再是错误,因为
undefined
将被替换为默认值。 但是,从调用者的角度来看,该参数仍然是可选的,如果您现在生成一个声明文件,我们会吐出该声明这会导致错误。 那是个错误。
顺便说一句,我建议避免在非实现签名中破坏参数。 它们实际上是一个实现细节,即函数实现是对参数进行了结构分解还是仅使用属性访问,都不应该真正在乎调用者。 但是,在声明文件的情况下,因为我们没有要发出的实际参数名称,所以我们(不情愿地)确实发出了破坏模式,因此我们需要修复该错误。