function multiply(num: number): number {
console.log(num * 10) // NaN
return num * 10
}
multiply("not-a-number") // result == NaN
如果我试图通过硬编码无效的参数类型来调用上面的函数,Typescript会报错。这是意料之中的。
当参数被动态传递时,我期望的是同样的事情。
const valueFromDifferentSource = "not-a-number"
multiply(valueFromDifferentSou
据我所知,TypeScript允许为任何类型创建别名。例如:
type MyNum = number;
var y: MyNum;
y = 33; // it's ok
但以下代码无效:
type MyType = Object;
const dt = new MyType(); // here is an error: 'MyType only refers to a type, but is being used as a value here'
我错在哪里,如何创建MyType实例?
下面是我在使用typescript时观察到的总结。 下面是一些代码: type someTypeEnum = '1';
type someOtherTypeEnum = '2' | '3';
type combinedTypeEnum = someTypeEnum | someOtherTypeEnum; 这是第一个案例: function typeAssertion<T extends combinedTypeEnum>(args: T): args is someTypeEnum {
// The error i get
当我们输入断言时,如果Typescript知道我们不可能是对的,它就会报错。例如 type Counter = (start: number) => number;
let counterProblemDetected = ((start: number) => "yo") as Counter; 但是当非空检查被启用时,它不会像我们设置了类型时那样抱怨没有从函数返回: // 'strictNullChecks' is on
// Typescript does not complain
let counterProblemNotDetected
我有一个简单的对象,有两个字段name和age,最初它们被定义为未定义的,稍后在代码中将赋值 const s={
name:undefined,
age:undefined
}
s.name="Test" //error 当我尝试为我的属性赋值时,它会出现typescript错误 Type '"Test"' is not assignable to type 'undefined'. 有人能解释为什么我在typescript中遇到这个错误以及如何修复它吗?
我假设这是故意的行为,但这是出乎意料的。鉴于以下情况:
interface Foo {
foo: number
}
interface Bar {
bar: Foo
}
const BAR = 'bar'
const BAR2: string = 'bar'
function getBar(bar: Bar): boolean {
return bar[BAR].foo // error, typescript can discern the value of `BAR` and reports that `number` is not ass
为什么在这里我得到的resourceInfo是空的。有没有其他方法来获取资源类和资源方法?
@Provider
@Authenticate
@Transactional
public class TestFilter2 implements ContainerRequestFilter {
private final SessionRepository sessionRepository;
@Context
public ResourceInfo resourceInfo;
@Inject
public TestFilter2(SessionRepository se
我正在为具有依赖关系的服务创建一个简单的单元测试。这是一个简单的服务,我正在开始帮助我的头脑围绕着最好的过程和结构。
我已经创建了单元测试,但是TypeScript抛出了一个错误。
Argument of type '{ order_id: string; user: string; }' is not assignable to parameter of type 'Expected<Observable<any>>'.
Object literal may only specify known properties, and '
我希望在下面的代码中有一个类型错误,但是typescript是完全可以的,你能告诉我为什么吗?
export interface Structure {
aaa: string;
}
export function f1(): Structure[] { // OK for typescript, not for me
const result = [].map(certState => {
return {
aaa: 'aaa',
ADDITIONAL_FIELD: 'asdf&
我已经用TypeScript开发了几个月,现在正在研究是否值得为我的开发迁移到。我遇到的问题之一是在使用外部模块和注入服务时的重复。例如,我有以下(简化)代码:
///<reference path="../../../../typings/tsd.d.ts" />
// imports for strong typing
import audioModel = require('app/plugins/audio/audio.model');
export class AudioController {
public model: a
我有以下TypeScript代码: class ClassA {
field1: any;
}
const SomeClassAsA = diContainer.get('SomeClass') as ClassA;
class ClassB extends ClassA {} // works
class ClassB extends SomeClassAsA {} // error: Type ClassA is not a constructor function type 为什么可以用ClassA扩展ClassB,而不能用其他被转换为ClassA的对象来扩展呢
以下typescript代码导致错误(typescript v3.8.3)。为什么? class A {
public foobar: 'foo' | 'bar' = 'bar';
}
class B extends A {
public foobar = 'foo'; // --> Type 'string' is not assignable to type '"foo" | "bar"'
} 游乐场:https://www.types
如果需要,您可以克隆代码库,使用TypeScript进行编译,然后在Here is the repo for this question中运行它,以查看是否存在运行时错误,但没有编译时错误。 切入正题:假设对象没有方法,当我尝试运行从另一个模块file2导入的对象的方法时,为什么我在file1中得不到TypeScript的编译时错误? 这是代码,基本上: 要设置项目,请执行以下操作: mkdir test
cd test
npm init --yes
yarn add typescript
touch tsconfig.json
touch index.ts
touch jsModule.j
第一次学习angular和typescript,不明白为什么这是无效的!
http.service.ts
export class HttpService {
constructor(private http: HttpClient) { }
getBeer() {
return this.http.get('https://api.openbrewerydb.org/breweries')
}
}
该api返回一个对象
the component its being used in:
export class ListComponent imp