我有一些代码:
baseTypes.ts
export namespace Living.Things {
export class Animal {
move() { /* ... */ }
}
export class Plant {
photosynthesize() { /* ... */ }
}
}
Run Code Online (Sandbox Code Playgroud)
dog.ts
import b = require('./baseTypes');
export namespace Living.Things {
// Error, can't find name 'Animal', ??
export class Dog extends Animal {
woof() { }
}
}
Run Code Online (Sandbox Code Playgroud)
tree.ts
// Error, can't use the same name twice, ??
import b = require('./baseTypes');
import b = require('./dogs');
namespace Living.Things {
// Why do …Run Code Online (Sandbox Code Playgroud) 我写了一些代码:
function renderGreeting(Elem: React.Component<any, any>) {
return <span>Hello, <Elem />!</span>;
}
Run Code Online (Sandbox Code Playgroud)
我收到一个错误:
JSX元素类型
Elem没有任何构造或调用签名
这是什么意思?
我不确定在TypeScript中处理"this"范围的最佳方法.
这是我转换为TypeScript的代码中常见模式的示例:
class DemonstrateScopingProblems {
private status = "blah";
public run() {
alert(this.status);
}
}
var thisTest = new DemonstrateScopingProblems();
// works as expected, displays "blah":
thisTest.run();
// doesn't work; this is scoped to be the document so this.status is undefined:
$(document).ready(thisTest.run);
Run Code Online (Sandbox Code Playgroud)
现在,我可以将呼叫改为......
$(document).ready(thisTest.run.bind(thisTest));
Run Code Online (Sandbox Code Playgroud)
......确实有效.但它有点可怕.这意味着代码可以在某些情况下编译和工作正常,但如果我们忘记绑定范围,它将会中断.
我想在类中做一个方法,这样在使用类时我们不需要担心"this"的作用范围.
有什么建议?
另一种方法是使用胖箭头:
class DemonstrateScopingProblems {
private status = "blah";
public run = () => {
alert(this.status);
}
}
Run Code Online (Sandbox Code Playgroud)
这是一种有效的方法吗?
TypeScript有许多不同的方法来定义枚举:
enum Alpha { X, Y, Z }
const enum Beta { X, Y, Z }
declare enum Gamma { X, Y, Z }
declare const enum Delta { X, Y, Z }
Run Code Online (Sandbox Code Playgroud)
如果我尝试从使用的值Gamma在运行时,我得到一个错误,因为Gamma没有定义,但事实并非案例Delta或Alpha?这里的声明是什么const或declare意味着什么?
还有一个preserveConstEnums编译器标志 - 这与这些标志如何相互作用?
我有一些TypeScript文件:
MyClass.ts
class MyClass {
constructor() {
}
}
export = MyClass;
Run Code Online (Sandbox Code Playgroud)
MyFunc.ts
function fn() { return 0; }
export = fn;
Run Code Online (Sandbox Code Playgroud)
MyConsumer.ts
import * as MC from './MyClass';
import * as fn from './MyFunc';
fn();
Run Code Online (Sandbox Code Playgroud)
这使我在尝试使用时出错 new
模块"MyClass"解析为非模块实体,无法使用此构造导入.
和试图打电话时 fn()
无法调用类型缺少调用签名的表达式.
是什么赋予了?
我有一些代码:
enum Color {
Red,
Green,
Blue
}
function getColorName(c: Color): string {
switch(c) {
case Color.Red:
return 'red';
case Color.Green:
return 'green';
// Forgot about Blue
}
throw new Error('Did not expect to be here');
}
Run Code Online (Sandbox Code Playgroud)
我忘了处理这个Color.Blue案子,我宁愿遇到编译错误.如何构造我的代码,以便TypeScript将此标记为错误?
我刚刚从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 中,您可以将函数注释为返回void:
function fn1(): void {
// OK
}
function fn2(): void {
// Error
return 3;
}
Run Code Online (Sandbox Code Playgroud)
您还可以注释要返回的函数undefined:
function fn3(): undefined {
// OK
return;
}
function fn4(): undefined {
// Error
return 3;
}
Run Code Online (Sandbox Code Playgroud)
所以看起来如果你调用一个返回的函数void,你总是会得到值undefined。但是你不能写这个代码:
function fn5(): void {
}
let u: undefined = fn5(); // Error
Run Code Online (Sandbox Code Playgroud)
为什么不void只是 的别名undefined?它真的需要存在吗?
标题说明了一切-为什么Object.keys(x)在TypeScript 中不返回类型Array<keyof typeof x>?就是这样Object.keys做的,因此对于TypeScript定义文件作者来说,似乎很明显的疏忽是不将返回类型简单地设为keyof T。
我应该在他们的GitHub存储库上记录错误,还是继续发送PR为其进行修复?
这三个函数似乎做同样的事情,但最后一个是错误.为什么会这样?
interface StringMap {
[key: string]: string;
}
function a(): StringMap {
return { a: "1" }; // OK
}
function b(): StringMap {
var result: StringMap = { a: "1" };
return result; // OK
}
function c(): StringMap {
var result = { a: "1" };
return result; // Error - result lacks index signature, why?
}
Run Code Online (Sandbox Code Playgroud) typescript ×10
ecmascript-6 ×1
enums ×1
es6-modules ×1
javascript ×1
material-ui ×1
module ×1
reactjs ×1
this ×1
undefined ×1
void ×1