TypeScript keyof & typeof All In One
TypeScript keyof & typeof All In One
keyof
"use strict";
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2023-01-29
* @modified
*
* @description
* @difficulty Easy Medium Hard
* @ime_complexity O(n)
* @space_complexity O(n)
* @augments
* @example
* @link https://www.typescriptlang.org/docs/handbook/2/keyof-types.html
* @solutions
*
* @best_solutions
*
*/
export {};
const log = console.log;
// The keyof operator takes an `object type` and produces a `string` or `numeric literal union` of its keys.
type Point = { x: number; y: number };
const point: Point = {
x: 1,
y: 2,
}
type P = keyof Point;
// type P === type as “x” | “y” ❓
// const p: P = {
// x: 1,
// y: 2,
// }
// Type '{ x: number; y: number; }' is not assignable to type 'keyof Point'.ts(2322)
// const p: P = {x: 1} || {y: 2};
type Obj = {
[key in P]: string;
// 等价于
// [key in keyof Point]: string;
}
const obj: Obj = {
x: `1`,
y: `2`,
}
// const obj: Obj = {
// x: 1,
// y: 2,
// }
// Type 'number' is not assignable to type 'string'.ts(2322)
type TypeNumber = {
// number => number
[n: number]: unknown;
};
type TN = keyof TypeNumber;
// type TN = number
const num: TN = 2023;
type TypeMap = {
// JavaScript 对象键总是被强制转换为字符串 ✅
// JavaScript object keys are always coerced to a string, so `obj[0]` => `obj["0"]`
// number => number, string => number
[k: string]: boolean;
};
/*
type TypeMap = {
[k: string]: boolean;
}
*/
type TM = keyof TypeMap;
// type TM = string | number
const tmn: TM = 2023;
const tms: TM = `2023`;
https://www.typescriptlang.org/docs/handbook/2/keyof-types.html
typeof
"use strict";
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2023-01-29
* @modified
*
* @description TypeScript adds a typeof operator you can use in a type context to refer to the type of a variable or property
* @description TypeScript 添加了一个 `typeof` 运算符,您可以在`类型上下文`中使用它来引用`变量`或`属性`的类型
* @difficulty Easy
* @ime_complexity O(n)
* @space_complexity O(n)
* @augments
* @example
* @link https://www.typescriptlang.org/docs/handbook/2/typeof-types.html
* @solutions
*
* @best_solutions
*
*/
export {};
const log = console.log;
// JavaScript typeof
log(typeof "Hello world");
// "string"
log(typeof "Hello world" === "string");
// true
// TypeScript typeof
// let ✅
let str = "string";
const s: typeof str = `2023`;
// const s: string
// const ❌
const str1 = "string";
const s1: typeof str1 = `2023`;
// const s1: "string"
// Type '"2023"' is not assignable to type '"string"'.ts(2322) ❌
type Str2 = string;
const s2: typeof Str2 = `2023`;
// 'Str2' only refers to a type, but is being used as a value here.ts(2693) ❌
/*
This isn’t very useful for basic types, but combined with other type operators, you can use typeof to conveniently express many patterns.
这对基本类型不是很有用,但结合其他类型运算符,您可以使用 typeof 方便地表达许多模式。
*/
// the predefined type ReturnType<T>
// 预定义类型 ReturnType<T> ✅
/**
* Obtain the return type of a function type
*/
// type ReturnType<T extends (...args: any) => any> = T extends (...args: any) => infer R ? R : any;
type Predicate = (x: unknown) => boolean;
type RT = ReturnType<Predicate>;
// type RT = boolean
function func() {
return { x: 10, y: 3, };
}
// type FRT = ReturnType<func>;
// 'func' refers to a value, but is being used as a type here.Did you mean 'typeof func' ? ts(2749)
type FRT = ReturnType<typeof func>;
/*
type FRT = {
x: number;
y: number;
}
*/
/*
typeof 使用限制
TypeScript intentionally limits the sorts of expressions you can use typeof on.
Specifically, it’s only legal to use typeof on identifiers (i.e. variable names) or their properties.
TypeScript 有意限制了可以使用 typeof 的表达式的种类。
具体来说,只有在标识符(即`变量名`)或其`属性`上使用 typeof 才是合法的。
*/
/*
$ npx ts-node 00\ index.ts
*/
https://www.typescriptlang.org/docs/handbook/2/typeof-types.html
refs
https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-9.html
https://www.typescriptlang.org/docs/handbook/advanced-types.html
https://www.typescriptlang.org/docs/handbook/2/types-from-types.html
TypeScript
keyofAll In One
https://www.cnblogs.com/xgqfrms/p/16151185.html
(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!
©xgqfrms 2012-2021
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/14100325.html
未经授权禁止转载,违者必究!

浙公网安备 33010602011771号