相关疑难解决方法(0)

为什么我收到错误"对象文字可能只指定已知属性"?

我刚刚从TypeScript 1.5升级到最新版本,我在代码中看到错误:

interface Options {
   /* ... others ... */
   callbackOnLocationHash?: boolean;
}

function f(opts: Options) { /* ... */ }

//  Error: Object literal may only specify known properties,
//     and 'callbackOnLoactionHash'does not exist in type 'Options'.
f( { callbackOnLoactionHash: false });
Run Code Online (Sandbox Code Playgroud)

代码看起来很好.怎么了?

(另类宇宙版:我认识到错字,我真的很想写那个.我该怎么做才能删除错误?)

typescript

52
推荐指数
1
解决办法
6万
查看次数

Typescript中的可选类成员

有没有办法在Typescript类中指定类型安全的可选成员?

就是这样......

class Foo {
    a?: string;
    b?: string;
    c: number;
}

....

foo = new Foo();
...
if (foo.a !== undefined) { ... (access foo.a in a type-safe string manner) ... }
Run Code Online (Sandbox Code Playgroud)

如果您熟悉OCaml/F#,我正在寻找类似'string option'的东西.

typescript

19
推荐指数
3
解决办法
2万
查看次数

标签 统计

typescript ×2