先阅读这个问题可能会有所帮助:How to copy the structure of one generic type to another generic in TypeScript? 给定以下输入类型: interface InputType {
age: number;
surname: string;
} 我想要一个函数,它可以为上面的输入产生以下输出类型: type OutputValue<T> = (val: T) => void;
interface OutputType {
age: OutputValue<number&g
假设泛型函数有两种类型,它们的返回类型不同。一个是返回类型T,另一个是T[]
type F1 = <T>(t: T) => T
type F2 = <T>(t: T) => T[]
我想知道如何将这两种类型合并成一种泛型类型。我想象的方法是让这个新泛型接受另一个泛型类型作为参数(比如C++的):
// Caution, Fantasy-TypeScript ahead!
// Accepts generic type R as parameter
// to transform return type of generic function
type F&
以下代码会导致错误,因为根据TypeScript,类型Pick<Test<T>, Exclude<keyof T, "someKey">>不能赋值给T。当我推断这种类型时,它应该被赋值给T,有没有办法让TypeScript同意我的观点?或者这只是突破了TypeScript中泛型类型的界限,因为不是动态类型的变体可以正确编译。 type Test<T = {}> = T & {
someKey: string
};
function extract<T>(props: Test<T>): T
是否可以将typescript联合类型与表单对象类型的值重新映射? 例如我在mid中的: type Union = 'item-type' | 'category';
type Special = {
'item-type': 'itemType';
};
type RemapedValue = [... some realy awesome typescript ...] // => 'itemType' | 'category'
我在RN项目中有样式化的组件和typescript Typescript在我的一个文件中抛出了一个奇怪的错误,上面写着Element implicitly has 'any' type because expression of type 'string' can't be used to index 'ColorKeyType' 受影响的代码,错误在return theme.colors[color]部分 const StyledText = styled(Text)<TextStyleProps>`
color: $
如何访问和默认不存在的对象属性?
在此示例中,typescript抱怨fooBar[foo] - Element implicitly has an 'any' type because expression of type '"fooBar"' can't be used to index type '{ readonly foo: "bar"; readonly bar: "foo"; }'. Property 'fooBar' does not exist on t
我正在使用TypeScript中的专用函数签名。我的理解是,以下几点应该有效:
// Basic superclass and subclass
class Model { }
class Product extends Model { name: string; }
// Define interface for singleton factory class with generic create method.
interface IModels { create(type: any): Model; }
const ctors = {product: Product};
// I
是否有方法在类型记录中创建包含基于参数类型的属性的Generic类型?
我正在寻找的是类似于映射类型的东西。但是,我不使用属性名称,而是使用类型名称。
用例。我看到很多API返回以下结构
{
"total": 100,
"skip": 0,
"limit": 30,
"a property named based on the type being queried": [
// the actual data requested
]
}
我想做的是创建一个能够接收数据的泛型。我在想
interface
在TypeScript中,私有属性被认为是类型的形状(或接口)的一部分。
class Person {
constructor(private name: string, public age: number) { }
}
const p: Person = { age: 42 };
// Error: Property 'name' is missing.
这是有效的,因为TypeScript需要跟踪隐私。
class Person {
constructor(private name: string, public age: number) { }
equals
Pick类型包括在TypeScript中。它的实施情况如下:
type Pick<T, K extends keyof T> = {
[P in K]: T[P];
};
如何编写PickByValue类型以使下列工作:
type Test = {
includeMe: 'a' as 'a',
andMe: 'a' as 'a',
butNotMe: 'b' as 'b',
orMe: 'b' as 'b'
};
type Inclu
我一直在研究并尝试将现有项目(从Node.js)转换为TypeScript。
对于上下文,我使用的是http-status包()
我试图将变量传递到它们的默认导出中,但是它得到了一个错误:
import status = require("http-status");
status.OK; // this works
status["OK"] // this also works
let str = "OK";
status[str]; // error
错误:
元素隐式具有'any‘类型,因为类型'string’的表达式不能
在TypeScript中有没有一种方法可以直接从对象的键创建一个新类型?在下面的示例中,TypeScript正确地抱怨('msg' refers to a value, but is being used as a type here.) msg引用了一个值,但我想知道是否有一种方法可以在不创建单独的类型定义的情况下使其正常工作? const msg = {okay: 'Okay', cancel: 'Cancel'};
function getMsg(id: keyof msg): string {
return msg[id];
}