gpt4 book ai didi

javascript - 即使在将数字显式转换为字符串变量后,Angular 仍然编译没有错误

转载 作者:太空狗 更新时间:2023-10-29 19:33:34 28 4
gpt4 key购买 nike

我目前正在学习 Angular 第一个主题..不幸的是我已经遇到了一个不寻常的场景..像这样

 import { Component } from '@angular/core';
@Component({
selector: 'sandbox',
template: `
<h1>Hello {{ name }}</h1>`
})
export class SanboxComponent {
name:string = "John Doe"; //name is declared as string
constructor() {
this.name = 34; //this should be the error..
}
}

但是,在我的浏览器上,它仍然输出“Hello 34”。

我懂javascript,但这也是我第一次接触typescript。根据我的理解,name:string 应该只接受一个字符串作为值。有人可以解释这里发生了什么吗?

最佳答案

默认情况下,TypeScript 转译器会返回一个警告,但转译会继续,即使出现类型错误也是如此。

如果你想更准确,并防止转译器处理类型错误,你可以编辑你的 tsconfig.json

{
"compilerOptions": {
"noEmitOnError": true,
}
}

noEmitOnError 设置为 true

关于javascript - 即使在将数字显式转换为字符串变量后,Angular 仍然编译没有错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48543966/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com