简体   繁体   English

为什么 TypeScript 不能从 function 签名推断类型?

[英]Why TypeScript doesn't infer type from function signature?

I've an issue in my Angular service with TypeScript type inferance.我的 Angular 服务存在 TypeScript 类型推断的问题。 I'm new to TypeScript and I wonder why this code:我是 TypeScript 的新手,我想知道为什么这个代码:

initializeTransaction(user: User, amount: number) {
  let params = {
    userId: user.id,
    amount: amount
  };
  return this.http.post<any>('/api/v1/payment/transaction/initialize', params);
}

where:在哪里:

export class User {
  id: number;
}

gives me this payload:给了我这个有效载荷:

{"userId":1,"amount":"2000"}

Shouldn't amount be a number? amount不应该是数字吗?

Edit: Here is the context of PaymentComponent:编辑:这是 PaymentComponent 的上下文:

export class PaymentComponent implements OnInit {

  private user = new User();

  submitted = false;
  redirecting = false;
  paymentForm: FormGroup;

  constructor(
    private formBuilder: FormBuilder,
    private paymentService: PaymentService,
    private userService: UserService
  ) {
    this.userService.me().subscribe(response => this.user = response);
  }

  ngOnInit() {
    this.paymentForm = this.formBuilder.group({
      amount: ['', [Validators.required, Validators.pattern("^[0-9]*$")]]
    });
  }

  get f() { return this.paymentForm.controls; }

  onPay() {
    this.submitted = true;

    if (this.paymentForm.invalid) {
      return false;
    }

    this.redirecting = true;
    this.paymentService.initializePage(this.user, this.f.amount.value)
      .subscribe(response => window.location.href = response.redirectUrl);
  }

}

the payload is interpreted in pure Javascript at runtime... so whatever the backend response has would be set in the variable.有效负载在运行时以纯 Javascript 解释......所以无论后端响应有什么都将在变量中设置。

the angular services and TypeScript itself do not remove the additional attributes from the json payload. angular 服务和 TypeScript 本身不会从 json 有效负载中删除附加属性。

you can either change your backend to not serve the additional attributes you wouldn't want to have or customize your service to remove the attributes.您可以更改您的后端以不提供您不希望拥有的其他属性,或者自定义您的服务以删除这些属性。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么 typescript 不能推断 RxJs stream 中的元组返回类型? - Why can't typescript infer tuple return type in RxJs stream? 当您将返回类型方法的 output 分配给变量时,为什么 Typescript 无法推断类型? - Why Typescript can't infer type when you assign a return typed method's output to a variable? 输入'字典<t> ' 没有呼叫签名。 TypeScript,ngRx</t> - Type 'Dictionary<T>' has no call signature. TypeScript, ngRx 如果我们在没有括号的情况下调用 function,为什么 Typescript 不会抛出错误? - Why doesn't Typescript throw an error if we call a function without parenthesis? 从 HTML 调用打字稿文件中的函数不显示返回变量 - Calling a function in a typescript file from HTML doesn't show the returning variable 为什么不能从Typescript类的实例成员函数或方法中获取值? - Why can't I get a value from an instance member function or method from typescript class? Typescript 类型安全 - 服务不返回指定的 class object - Typescript type safety - service doesn't return specified class object Typescript 迭代 object 似乎不起作用(类型错误) - Typescript iterating over object doesn't seem to work (Type Error) 打字稿抱怨 PostResponse[] 类型上不存在属性名称 - Typescript complains property name doesn't exist on type PostResponse[] 类型 any[] 上不存在打字稿 flatMap、flat、flatten - Typescript flatMap, flat, flatten doesn't exist on type any[]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM