我有一个函数URLBuilder,它作为一个模块工作(它返回一个包含各种函数的对象,这些函数与上下文对象绑定得很好,所以this绑定按预期工作,我们可以在这些函数中正确地访问this ) 我的问题是为什么typescript不理解URLBuilder的返回对象中函数的this绑定?尽管我尝试手动添加一个返回类型,但Typescript抱怨when using the URLBuilder在没有this上下文的情况下无法接受函数。 我们如何让typescript在返回的对象中理解这个绑定函数的this? 我要解决的Typescript错误是: The 'this' contex
当我们输入断言时,如果Typescript知道我们不可能是对的,它就会报错。例如 type Counter = (start: number) => number;
let counterProblemDetected = ((start: number) => "yo") as Counter; 但是当非空检查被启用时,它不会像我们设置了类型时那样抱怨没有从函数返回: // 'strictNullChecks' is on
// Typescript does not complain
let counterProblemNotDetected
app.put('/update-user/:id', (async (req: Request, res: Response) => {
const { id } = req.params;
const filter = { _id: new ObjectId(id) };
const user = req.body // Here data type is needed and typescript is showing error
console.log(user);
我正在使用VS Code,它在使用Typescript时非常有用,因为它告诉您当前的问题不需要传输。我使用的是Typescript和nodeJS,它工作得很好。
我唯一的问题就是所谓的“空/未定义的检查函数”。
参见此示例:
class User {
public someProperty: string | undefined;
// ...
public get canDoSomething() {
return this.someProperty != undefined;
}
}
let myUser: User = ...
这样做很好:
if (
我有这样的代码:
class A {
public var1: string = 'var1';
}
class B {
public var1: string = 'var1';
public var2: string = 'var2';
}
const instance: A = new B();
console.log(instance instanceof A);
我不明白为什么typescript编译器可以使用instance: A = new B(),因为B没有扩展A。如果A和B是接口,我可以理解,因为接口
TypeScript使用,所以这实际上是可能的:
// there is a class
class MyClassTest {
foo():void{}
bar():void{}
}
// and a function which accepts instances of that class as a parameter
function someFunction(f:MyClassTest):void{
if (f){
if (!(f instanceof MyClassTest)) {
console.log("
我试图使用TypeScript定义一个API,这样它就可以像这样工作了:
// Create new user (Working)
var user : IUser = new Api.User({ firstName: "John", lastName: "Smith" });
// Delete existing user (Working)
Api.User.Delete(1);
// Load existing user (Unsure how to implement)
var user = Api.User(123);
我的TypeScript:
我试图使用汇总构建一个带有Svelte和TypeScript的应用程序,而当我试图构建Svelte组件时,我似乎无法让它编译.ts组件中包含的.ts文件。
我一直在犯这个错误:
[!] Error: Unexpected token (Note that you need plugins to import files that are not JavaScript)
src/ui/pages/files-page/utils/mapPaths.ts (1:12)
1: import type { PathMapping } from '~/types/path.d';
为什么S在下面的代码中是string?
type T<X> = {[K in keyof X]: never};
type S = T<string>;
这是TypeScript游乐场。
即使我用其他类型替换never,它也会变成相同的类型(never)。
添加上下文,下面的类型将如我所期望的那样工作。
type T<X> = {[K in (keyof X) & string]: never};
type S = T<string>;
当我做流星建造…时/output --仅用于服务器部署,我得到了错误:this.compilerCli.createCompilerHost is not a function
我将Meteor@1.5.4.1项目升级为Meteor@1.6.1.1,删除了angular2编译器并添加了角编译器,并进行了相关的更改。然而,该项目正在完美地运行,但是,用于部署的构建抛出了错误。流星版本为1.5.4.1,但没有问题。
Ubuntu版本:Ubuntu17.10
Meteor版本: METEOR@1.6.1.1
角版: 4.4.7
edge@edge-VirtualBox:~/Repos/webapp-
我试图通过查看这个问题和本文中的来配置vscode来编译类型记录,但是得到了一个错误。需要帮助。
我的项目树:
文件server.ts --这是我想要编译的文件,但是以后我会有更多的.ts文件。
这是我的tasks.json
{
"version": "0.1.0",
// The command is tsc. Assumes that tsc has been installed using npm install -g typescript
"command": "tsc",
/