我刚刚从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类中指定类型安全的可选成员?
就是这样......
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 ×2