Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

typescript 常用泛型 #22

Open
myml opened this issue Jan 23, 2019 · 0 comments
Open

typescript 常用泛型 #22

myml opened this issue Jan 23, 2019 · 0 comments
Labels

Comments

@myml
Copy link
Owner

myml commented Jan 23, 2019

预定义对象

class Test {
	a: string;
	b: number;
	c() {}
}

类型为对象键名

type ObjectKey<T> = keyof T  

 ObjectKey<Test> = 'a' | 'b' | 'c'  

类型为对象属性类型

type ObjectValue<T> = T[keyof T] 

ObjectValue<Test> = string | number | (() => void)  

转换对象属性为可选

type ObjectPartial<T> = { [K in keyof T]?: T[K] };  

ObjectPartial<Test> =  
	class Test {
		a?: string;
		b?: number;
		c?() {}
	}

去除对象属性

type ObjectDelete<T, D extends keyof T> = { [K in Exclude<keyof T, D>]: T[K] };

ObjectDelete<Test> =  
	class Test {
		b: number;
		c() {}
	}

用对象属性类型过滤属性

type ObjectKeyFilterByValue<T, V> = { [K in keyof T]: T[K] extends V ? K : never }[keyof T];

例子

ObjectKeyFilterByValue<Test,string> = { a: string }
@myml myml changed the title typescript 常用泛型定义 typescript 常用泛型 Jan 23, 2019
@myml myml added the web label Dec 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant