我写了这段代码
interface Foo {
    abcdef: number;
}
let x: Foo | string;
if (x instanceof Foo) {
    // ...
}
Run Code Online (Sandbox Code Playgroud)
但是TypeScript给了我这个错误:
'Foo' only refers to a type, but is being used as a value here.
Run Code Online (Sandbox Code Playgroud)
为什么会这样?我认为instanceof可以检查我的值是否具有给定类型,但TypeScript似乎不喜欢这样.
当我编译时,我在RxJS声明文件中得到以下编译器错误:
node_modules/rxjs/Subject.d.ts(16,22): error TS2415: Class 'Subject<T>' incorrectly extends base class 'Observable<T>'.
  Types of property 'lift' are incompatible.
    Type '<R>(operator: Operator<T, R>) => Observable<T>' is not assignable to type '<R>(operator: Operator<T, R>) => Observable<R>'.
      Type 'Observable<T>' is not assignable to type 'Observable<R>'.
         Type 'T' is not assignable to type 'R'.
Run Code Online (Sandbox Code Playgroud)
这里发生了什么,如何在不降级到TypeScript 2.3或更早版本的情况下解决这个问题?
interface Foo { 
    [foo: "hello" | "world"]: string;
}
Run Code Online (Sandbox Code Playgroud)
我收到一条错误消息
An index signature parameter type cannot be a union type. Consider using a mapped object type instead.
Run Code Online (Sandbox Code Playgroud)
什么是映射对象类型,以及如何使用它?
我使用的是打字稿,我想用一个作用域包(例如@foo/bar,@babel/core等)不出货了自己的类型声明.
我试过运行类似的东西
npm install @types/@foo/bar
Run Code Online (Sandbox Code Playgroud)
但似乎没有.
有没有办法让.d.ts这些包进入@types范围?如果需要,有没有办法在DefinitelyTyped上编写我自己的作用域包?
我正在使用一个foo在DefinitelyTyped上不存在的npm包.换句话说,@types/foo不存在(或可能已过时!)
我仍然希望能够在更严格的设置下使用它noImplicitAny,所以我需要自己编写自定义定义文件.最后,我想向DefinitelyTyped发送一个pull请求,以便此文件对我项目之外的其他人有用.
有很简单的解决方案,比如创建一个名为的全局文件./src/types.d.ts,我可以编写以下内容
declare module "foo" {
    export function hello(): void;
    export function world(): void;
}
Run Code Online (Sandbox Code Playgroud)
但是如果我使用那种语法,当我将它提交给DefinitelyTyped时,我可能需要重写我的模块.
如何构建我的项目以便我可以轻松地创作然后将本地.d.ts文件发送到DefinitelyTyped?
在使用打字稿创建我的反应应用程序时,我遇到了一个我尚未能解决的小问题.
我的代码:
App.tsx
import  * as React from 'react';
import * as ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import { createStore, applyMiddleware } from 'redux';
import { BrowserRouter, Route, Switch } from 'react-router-dom';
import * as promise from 'redux-promise';
import reducers from './reducers';
import TemplateNavTop from './components/layout/template-nav-top';
const TestComponent2 = () => {
  return <h1>TestComponent</h1>;
}
const createStoreWithMiddleware = applyMiddleware(promise)(createStore);
ReactDOM.render(
<Provider store={createStoreWithMiddleware(reducers)}>
    <BrowserRouter>
        <Switch>
            <Route path="/" exact component={TestComponent} />
            <Route path="/checkout">
                <TemplateNavTop>
                    <TestComponent2 />
                </TemplateNavTop>
            </Route> …Run Code Online (Sandbox Code Playgroud) 我在用着:
$ tsc --version
Version 2.0.8
Run Code Online (Sandbox Code Playgroud)
并尝试将--experimentalDecorators 选项传递给tsc这样的:
$ tsc --experimentalDecorators true greeter.ts
//error TS6053: File 'true.ts' not found.
Run Code Online (Sandbox Code Playgroud)
像这样
$ tsc greeter.ts --experimentalDecorators true
//error TS6053: File 'true.ts' not found.
Run Code Online (Sandbox Code Playgroud)
像这样
$ tsc greeter.ts --experimentalDecorators=true
// error TS5023: Unknown compiler option 'experimentaldecorators=true'.
Run Code Online (Sandbox Code Playgroud)
但没有运气。将选项传递给 的正确方法是什么tsc?
// Input
class Foo {
    templateString = 'some value';
}
// Output
import __LIB__ from '@scope/lib/path/to/lib';
class Foo {
    templateString = (function compiledTemplate(deps) {
        // ...
        return result;
    })({lib: __LIB__});
}
Run Code Online (Sandbox Code Playgroud)
其实我有两个疑问:
PS我尝试了各种方法createSourceFile,ts.createImportDeclaration但它们都会导致这个或那个错误:[
我有这个代码:
const BlockConstructors: Function[] = [
 OBlock,
 IBlock,
 TBlock
];
function randomFromArray(array: any[]) {
  return array[Math.floor( Math.random() * array.length )];
}
const BlockConstructor: Function = random(BlockConstructors);
const block: Block = new BlockConstructor();
Run Code Online (Sandbox Code Playgroud)
我尝试从数组中绘制一些块构造函数,然后创建一个新对象,数组中的所有块构造函数都扩展了 Block 类。我得到错误:
不能将“new”用于类型缺少调用或构造签名的表达式。
为什么会出现这个错误?
我正在描述一个React库,它通过一个名为的属性获取组件或HTML标记名称as.给定as属性时,它会从该组件/标记名称创建一个元素,并传递任何其他给定的属性.
这里有些例子:
<Foo as="a" href="https://example.com" />
<Foo as={FancyButton} fancyButtonAttr="hello!" />
Run Code Online (Sandbox Code Playgroud)
我知道Semantic UI与增强功能类似.我如何在TypeScript中输入?
我的项目使用像@types/express和的包@types/body-parser.最近这些.d.ts文件被更新为使用通用默认值,但这意味着这些声明文件现在需要TypeScript 2.3或更高版本.
但是,我的项目仍然使用旧版本的TypeScript - TypeScript 2.2.如何在不升级的情况下使用npm确保我的项目仍能理解这些定义文件?
打字稿中枚举的用途是什么。如果它的目的只是为了使代码可红,我们就不能为了同样的目的使用常量吗?
enum Color {
Red = 1, Green = 2, Blue = 4
};
let obj1: Color = Color.Red;
obj1 = 100; // does not show any error in IDE while enum should accept some specific values
Run Code Online (Sandbox Code Playgroud)
如果没有typechecking的优势,难道就不能这么写。
const BasicColor = {
    Red: 1,
    Green: 2,
    Blue: 4
};
let obj2 = BasicColor.Red;
Run Code Online (Sandbox Code Playgroud) 说我有以下代码:
val a: List[(Int, String)] = List((1,"A"),(2,"B"),(3,"C"))
val b: List[String] = List("A","C","E")
Run Code Online (Sandbox Code Playgroud)
我可以:
a.map{case (fst,snd) => (fst,snd + "a")}
a.filter{case (_,snd) => b.contains(snd)}
Run Code Online (Sandbox Code Playgroud)
但为什么我不能这样做:
a.map((_._1,_._2 + "a"))
a.filter(b.contains(_._2))
Run Code Online (Sandbox Code Playgroud)
有没有办法用下划线表示法完成此操作,还是我被迫在这里?
typescript ×12
npm ×3
javascript ×2
reactjs ×2
types ×2
instanceof ×1
jsx ×1
node.js ×1
react-router ×1
rxjs ×1
rxjs5 ×1
scala ×1
tsc ×1