我有一个简单的对象,有两个字段name和age,最初它们被定义为未定义的,稍后在代码中将赋值 const s={
name:undefined,
age:undefined
}
s.name="Test" //error 当我尝试为我的属性赋值时,它会出现typescript错误 Type '"Test"' is not assignable to type 'undefined'. 有人能解释为什么我在typescript中遇到这个错误以及如何修复它吗?
在我的TypeScript 2.0代码中(在Visual 2015中),我有:
var lang = navigator.language
? navigator.language
: navigator.systemLanguage;
有了这个,我得到了一个错误:
Build:Property 'systemLanguage' does not exist on type 'Navigator'.
但是,如果我将它添加到我的一个d.ts文件中:
interface Navigator {
systemLanguage
你知道如何才能做到这一点吗?
function getTest<T>(): T["test"] {
// ...
}
...or动态:
function getTest<T, U>(): T[U] {
// ...
}
不幸的是,编译器说是Type '“test”' cannot be used to index type 'T'。我使用的是TypeScript 2.4.2。
我正在设置状态,以便在React中通过上下文传递(使用钩子)。在使用dispatched状态更新器函数时,我得到以下错误:
Cannot invoke an expression whose type lacks a call signature. Type 'number | Dispatch<SetStateAction<number>>' has no compatible call signatures
我是TypeScript的新手,我想我可能在某个地方错过了一个类型,但是我被?卡住了
最初,我试图传递defaultValue (在我的例子中是
我在Nim中看到了这个例子,它需要明确的int和bool类型规范,尽管有足够的信息来推断两者(就像TypeScript那样)。
如果删除了这些类型,它就不会编译,Nim支持类型推断吗?
import sequtils
let list = @[1, 2, 4]
echo list.filter do (x: int) -> bool: x > 2
我正在尝试从Typescript中的一个功能创建一个<GeoJSON {...} /> react元素,但没有成功。(我使用的是react-leaflet + Typescript)对于常规的js,我只需要这样做:
<Map
{...}
<GeoJSON
data={...} <- Here I put my feature in JSON format
style={...} <- Associated style
/>
/>
但是在Typescript中,如果我尝试做完全相同的事情,
我有一个接受两个参数的函数。基本上我想让Typescript检查,第二个参数不能包含第一个参数中的键。TS Sandbox interface Todo {
title: string;
description: string;
}
// here I hardcoded the 'title' key on the Omit utility type. My question is, how to make this dynamic, based on the key on the left argument.
function test<T>
下面是一些打字脚本: let markdown = <HTMLTextAreaElement>document.getElementById(id);
let markdownoutput = <HTMLLabelElement>document.getElementById(output); 在上面的代码中,在使用element by id选择某些东西之前,我们声明了元素类型param.(<HTMLTextAreaElement>)它工作得很好。但是我们从中得到的好处是什么呢? 有人能帮我吗? typescript是如何处理这个问题并帮助我们的呢? 提前
我正在使用来自react-query的useQuery钩子,当我试图在查询函数中访问我的键时,也就是useQuery的第二个参数,我遇到了一个关于typescript的问题,它抱怨这个语句 const postId = queryKey[1].id as any 那 Property 'id' does not exist on type 'string | { id: string | undefined; comments: boolean; }'.
Property 'id' does not exist on type 's
有没有办法告诉TypeScript编译器不允许通过对象映射非法访问私有变量?
例如:
class Thing {
private id: number;
constructor() {
this.id = 1
}
}
var thing = new Thing()
alert(thing["id"]); // I want this to throw a compiler error
在typescript 2.5.2中,以下代码无法使用strictNullChecks进行编译
function ifnull<T>(x: T | null | undefined, def: T): T
{
return x != null ? x : def;
}
console.log(ifnull(2, 1));
给出错误:
Argument of type '2' is not assignable to parameter of type '1 | null | undefined'.
它似乎推断T是1而不是number。当不使
我有一个使用keyof特性的简单函数: interface B {
name: string;
age: number;
}
function test(key: keyof B) {}
const c = {
age: 'age'
}
test(c.age); 上面代码的问题是typescript抛出了一个错误,即type string is not assignable to keyof B。 那么,如果keyof特性不能与对象键值一起使用,那么它还有什么意义呢?我不想添加as keyof B。
很抱歉,我想输入一个mapBy
export function mapBy<T, K = keyof T>(array: T[], key: K): T[K][] {
return array.map((item: T) => item[key]);
}
TypeScript投诉:Type 'K' cannot be used to index type 'T'. ts(2536)
我的第二个案子更棘手:
export default function sumBy<T, K = keyof T>(array: T[], key:
我在将它们添加到一些现有的项目时遇到了一些困难。例如,我在我开发的模块中有一个类:
export default class ClassName {
// Class members
}
现在,我将它导入另一个项目:
import ClassName from 'modulename';
const object = new ClassName();
我在这条线上有两个错误。
论object in const object
error Unsafe assignment of an any value @typescript-eslint/
我希望使用process.env以通用方式解析值,例如TypeScript:
type SimpleType = string | number | boolean
function getEnv<T extends SimpleType>(name: string): T {
return process.env[name] as T
}
但是,as关键字在TypeScript中不提供类型转换(例如,在C#中)。
有没有一种通用的方法来做到这一点?
如果对象中的属性存在且大于0,则尝试将其减少1,但无论我尝试什么,TypeScript都不会编译这段代码。
interface IStatus {
[key: string]: number | undefined; // <= I don't want undefined here but it complains since the params are optional
burning?: number;
firing?: number;
}
class Thing {
public status: IStatus = {};
private proc
我不太理解TypeScript的Promise定义,如下所示: /**
* Represents the completion of an asynchronous operation
*/
interface Promise<T> {
/**
* Attaches callbacks for the resolution and/or rejection of the Promise.
* @param onfulfilled The callback to execute when the Promise is resolved.
*
我试图从Svelte和TypeScript中的日期对象中获得时间戳。我希望每当Date对象被更新时,时间戳都要更新,所以我试图使它具有反应性。下面是我尝试过的代码:
let date: Date = new Date();
$: timestamp: string = date.getHours() + ':' + date.getMinutes() + ":" +
date.getSeconds(); // timestamp in format hh:mm:ss
但是我从TypeScript:'string' only refers