小编ktr*_*yak的帖子

找不到模块'module-name'的声明文件.'/path/to/module-name.js'隐式具有'any'类型

我读了TypeScript 模块解析的工作原理.

我有以下存储库:ts-di.编译目录结构后如下:

??? dist
?   ??? annotations.d.ts
?   ??? annotations.js
?   ??? index.d.ts
?   ??? index.js
?   ??? injector.d.ts
?   ??? injector.js
?   ??? profiler.d.ts
?   ??? profiler.js
?   ??? providers.d.ts
?   ??? providers.js
?   ??? util.d.ts
?   ??? util.js
??? LICENSE
??? package.json
??? README.md
??? src
?   ??? annotations.ts
?   ??? index.ts
?   ??? injector.ts
?   ??? profiler.ts
?   ??? providers.ts
?   ??? util.ts
??? tsconfig.json
Run Code Online (Sandbox Code Playgroud)

在我的package.json中,我写道"main": "dist/index.js".

在Node.js中一切正常,但TypeScript:

import {Injector} from …
Run Code Online (Sandbox Code Playgroud)

node-modules typescript

199
推荐指数
19
解决办法
24万
查看次数

Angular 2:清理HTML使用div id删除了一些内容 - 这是错误还是功能?

<div [innerHTML]="body"></div>用来将未转义的HTML传递给我的模板,当我传递给body div属性时id,Angular throw:

警告:清理HTML会删除一些内容(请参阅http://g.co/ng/security#xss).警告:清理HTML会删除一些内容(请参阅http://g.co/ng/security#xss).警告:清理HTML会删除一些内容(请参阅http://g.co/ng/security#xss).

看到.plunker

那为什么这么说呢?什么可能是危险iddiv?可能这个bug?

security warnings html-sanitizing angular

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

Angular 5和Service Worker:如何从ngsw-config.json中排除特定路径

ngsw-config.json(摘自文档):

{
  "index": "/index.html",
  "assetGroups": [{
    "name": "app",
    "installMode": "prefetch",
    "resources": {
      "files": [
        "/favicon.ico",
        "/index.html"
      ],
      "versionedFiles": [
        "/*.bundle.css",
        "/*.bundle.js",
        "/*.chunk.js"
      ]
    }
  }, {
    "name": "assets",
    "installMode": "lazy",
    "updateMode": "prefetch",
    "resources": {
      "files": [
        "/assets/**"
      ]
    }
  }]
}
Run Code Online (Sandbox Code Playgroud)

在我的网站上有一个指向RSS源的链接,该链接/api/rss应该在新的浏览器选项卡中打开而不加载Angular应用程序.如何将其从请求重定向到的资源列表中排除index.html

UPD:我试过但没有使用以下配置(请参阅参考资料!/api/rss):

{
  "index": "/index.html",
  "assetGroups": [{
    "name": "app",
    "installMode": "prefetch",
    "patterns": ["!/api/rss"],
    "resources": {
      "files": [
        "/favicon.ico",
        "/index.html",
        "!/api/rss"
      ],
      "versionedFiles": [
        "/*.bundle.css",
        "/*.bundle.js",
        "/*.chunk.js"
      ]
    } …
Run Code Online (Sandbox Code Playgroud)

service-worker angular angular-service-worker

21
推荐指数
5
解决办法
7475
查看次数

promise.reject的TypeScript类型定义

以下代码在返回的类型方面是正确的,因为then始终返回promise数组.

Promise.resolve(['one', 'two'])
.then( arr =>
{
  if( arr.indexOf('three') === -1 )
    return Promise.reject( new Error('Where is three?') );

  return Promise.resolve(arr);
})
.catch( err =>
{
  console.log(err); // Error: where is three?
})
Run Code Online (Sandbox Code Playgroud)

TypeScript抛出错误:

类型参数'TResult'的类型参数不能从用法中推断出来.考虑明确指定类型参数.类型参数候选'void'不是有效的类型参数,因为它不是候选'string []'的超类型.

但实际上,then永远不会回归void.

我可以明确指定类型.then<Promise<any>>,但它更像是一种解决方法,而不是正确的解决方案.

怎么写这个吧?

typescript es6-promise

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

如何告诉TypeScript对象中的可选属性是否存在并设置?

我有以下代码:

interface First
{
  propertyA: string;
}

// Here propertyA is optional
// Imagine that this interface came from external library.
interface Second
{
  propertyA ?: string;
}

function fn(arg: First)
{
  // ...
}

// I know that this object can be declared as type of First,
// but I really need set this as type of Second interface
let myVar: Second = {propertyA: 'some string'};

// I really need in this way to make the call.
fn(myVar); // …
Run Code Online (Sandbox Code Playgroud)

typescript

7
推荐指数
4
解决办法
6510
查看次数

如何从另一个分支获取一个文件,但当前分支中不存在?

我对这个问题有类似的问题:https://stackoverflow.com/a/2364223/1716560

我有两个分支:ukuk-prod:

git checkout uk-prod
Already on 'uk-prod'

git diff --name-status uk-prod..uk
A       pages/Advanced Types.md
A       pages/Basic Types.md
A       pages/Classes.md
Run Code Online (Sandbox Code Playgroud)

我想只得到文件pages/Advanced Types.md:

git checkout uk -- "page/Advanced Types.md"
Run Code Online (Sandbox Code Playgroud)

但是git throw:

error: pathspec 'page/Advanced Types.md' did not match any file(s) known to git.
Run Code Online (Sandbox Code Playgroud)

据我所知,它发生是因为git没有看到本地文件page/Advanced Types.md.是否?

git git-checkout

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

TypeScript:匿名类工厂

我的 TypeScript v2.2。

我有这个班级工厂:

export class A { name: string; }

export function makeConstructor(name: string)
{
  const newClass = class extends A { };

  newClass.prototype.name = name;

  return newClass;
}
Run Code Online (Sandbox Code Playgroud)

打字稿抛出错误:

导出函数的返回类型具有或正在使用私有名称“(匿名类)”。

我可以说这个工厂返回any隐藏错误,但我如何解释究竟返回了什么?

我试着写

  • makeConstructor<T extends A>(name: string): T
  • makeConstructor<T extends typeof A>(name: string): T
  • makeConstructor<T extends A['prototype']>(name: string): T['prototype']

javascript generics typescript

5
推荐指数
1
解决办法
269
查看次数

如何告诉 TypeScript:获取`keyofimportObject`?

我的 TypeScript v2.2.1,我有对象列表my-module

export const OneObj = { prop1: 'value1' }
export const TwoObj = { prop2: 'value2' }
Run Code Online (Sandbox Code Playgroud)

我想在另一个模块中创建新类型:

import * as importedObject from './my-module';

console.log(importedObject)
// { OneObj: { prop1: 'value1' }, TwoObj: { prop2: 'value2' } }}

type NewType = keyof importedObject;
// Error: Cannot find name 'importedObject'
Run Code Online (Sandbox Code Playgroud)

为什么 TypeScript 会抛出错误?

找不到名称“importedObject”

同时,我可以这样做:

type NewType = keyof { OneObj: { prop1: 'value1' }, TwoObj: { prop2: 'value2' } };

// NewType === ("OneObj" | "TwoObj")
Run Code Online (Sandbox Code Playgroud)

typescript

4
推荐指数
1
解决办法
1015
查看次数

为什么 restify 没有自己的插件来支持 cookie?

我喜欢restify,它有很多粉丝,但为什么它没有自己的插件来支持 cookie?

也许我不知道一些东西,并且在使用 REST 架构时不需要使用 cookie?也许为此目的使用不同的方法?有没有可能已经有一个流行的模块Node.js,可以很容易地与restify集成?

cookies plugins node.js restify

2
推荐指数
1
解决办法
1392
查看次数

如何从 Angular 子组件内的“顶级组件”获取 DI?

来自角度文档

创建一个“顶级组件”,充当所有模块组件的根。将自定义 HttpBackend 提供程序添加到顶部组件的提供程序列表而不是模块的提供程序。回想一下,Angular 为每个组件实例创建一个子注入器,并使用组件自己的提供程序填充注入器。

当该组件的子组件请求 HttpBackend 服务时,Angular 会提供本地 HttpBackend 服务,而不是应用程序根注入器中提供的版本。无论其他模块对 HttpBackend 执行什么操作,子组件都会发出正确的 http 请求。

确保将模块组件创建为该模块顶部组件的子组件。

如果我们打开分层依赖注入器文档的源文件,我们可以看到ACarComponent,BCarComponent并创建,和CCarComponent的三个不同实例。CarServiceCarService2CarService3

但是,如果我也在模块级别上提供,如何CarService从父组件的 DI 中获取子组件呢?BCarComponentCarService

dependency-injection hierarchy web-component angular

2
推荐指数
1
解决办法
2107
查看次数

为什么在JavaScript类中有一个instanceof Function,但是typeof类不是一个对象?

当我们说"实例"时,我们假设我们正在处理一个对象.为什么JavaScript的运算符在我们要求时instanceof返回,但是?为什么不呢?true(class A { }) instanceof Functiontypeof (class A { }) == "function"object

javascript types

1
推荐指数
1
解决办法
517
查看次数