我正在尝试从Typescript中的一个功能创建一个<GeoJSON {...} /> react元素,但没有成功。(我使用的是react-leaflet + Typescript)对于常规的js,我只需要这样做:
<Map
{...}
<GeoJSON
data={...} <- Here I put my feature in JSON format
style={...} <- Associated style
/>
/>
但是在Typescript中,如果我尝试做完全相同的事情,
我从AWS放大器教程中复制了以下内容:
const styles = {
container: { flexDirection: 'column' },
...
};
<div style={styles.container}>
但我得到了:
Type '{ flexDirection: string; }' is not assignable to type 'CSSProperties'.
Types of property 'flexDirection' are incompatible.
在TypeScript中,我们使用上下文类型,当泛型类型未定义时,TypeScript将尝试从返回值推断正确的类型。
在大多数情况下,它可以正常工作。但有时会变得很奇怪。在我的示例中,当函数参数未定义时,有时不使用泛型默认类型,也不需要它。
示例&
type State = { count: number }
declare function simple<T = State>(second?: T): T;
const { count: countA } = simple()
// in 4.0+ => countA: number
// in 3.9 =&
我有一个使用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。
我有一个简单的对象,有两个字段name和age,最初它们被定义为未定义的,稍后在代码中将赋值 const s={
name:undefined,
age:undefined
}
s.name="Test" //error 当我尝试为我的属性赋值时,它会出现typescript错误 Type '"Test"' is not assignable to type 'undefined'. 有人能解释为什么我在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中,有没有办法键入以下fn函数,以便TypeScript仍然知道result的类型是'hello' const hello = 'hello' // hello is of type 'hello'
const fn = (x: unknown) => x
const result = fn(hello) // result is of type unknown 对于泛型,可以传递类型: const hello = 'hello' // hello is of type 'hello
我刚开始使用typescript,有javascript的背景。
在(现代) js中,以下情况是可能的:
let {n,s} = {n:42, s:'test'};
console.log(n, s); // 42 test
在typescript中,我假设我可以做同样的事情,事实上,根据的说法,声明和赋值都支持解构。除了我似乎不能对说
let {n:number, s:string} = {n:42, s:'test'}; // All destructured elements are unused.
console.log(n, s); // Cann
我正在设置状态,以便在React中通过上下文传递(使用钩子)。在使用dispatched状态更新器函数时,我得到以下错误:
Cannot invoke an expression whose type lacks a call signature. Type 'number | Dispatch<SetStateAction<number>>' has no compatible call signatures
我是TypeScript的新手,我想我可能在某个地方错过了一个类型,但是我被?卡住了
最初,我试图传递defaultValue (在我的例子中是
我有以下代码,它不会用TypeScript编译器3.7.3编译
type Fruit = 'apple' | 'banana'
const val = 'apple'
// Works. TS remembers the value of `val`
const fruit: Fruit = val
type Basket = {
fruit: Fruit
}
const obj = {
fruit: 'apple'
}
// Error: Type 'string' is not as
在运行使用Nuxt.js开发的Nuxt.js时,我遇到了一个非常奇怪的错误,它有Vue.js组件。也就是说,在运行应用程序时,我看到了与TypeScript相关的错误,比如TS2749: 'About' refers to a value, but is being used as a type here. Did you mean 'typeof About'?,尽管npm run test没有显示任何内容。
我的带有抱怨行的spec.ts文件
import { shallowMount, Wrapper } from "@vue/test-utils
我有一个用Typescript编写的$http服务,Webstorm Typescript文件观察者正在抱怨它。下面是这行代码
当我将鼠标悬停在"this“上或转到转译器窗口时,错误是
Error:(14, 20) TS2322: Type 'IPromise<void>' is not assignable to type 'IHttpPromise<IScheduleDay[]>'.
Property 'success' is missing in type 'IPromise<vo
我试图从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