[Typescript] Understanding Generics at Different Levels of Functions
The following code
import { expect, it } from 'vitest';
import { Equal, Expect } from '../helpers/type-utils';
export interface Cache<T> {
get: (key: string) => T | undefined;
set: (key: string, value: T) => void;
// You can fix this by only changing the line below!
clone: <U>(transform: (elem: T) => U) => Cache<U>;
}
const createCache = <T>(initialCache?: Record<string, T>): Cache<T> => {
const cache: Record<string, T> = initialCache || {};
return {
get: (key) => cache[key],
set: (key, value) => {
cache[key] = value;
},
clone: (transform) => {
const newCache: Record<string, any> = {};
for (const key in cache) {
newCache[key] = transform(cache[key]);
}
return createCache(newCache);
},
};
};
it('Should let you get and set to/from the cache', () => {
const cache = createCache<number>();
cache.set('a', 1);
cache.set('b', 2);
expect(cache.get('a')).toEqual(1);
expect(cache.get('b')).toEqual(2);
});
it('Should let you clone the cache using a transform function', () => {
const numberCache = createCache<number>();
numberCache.set('a', 1);
numberCache.set('b', 2);
const stringCache = numberCache.clone((elem) => {
return String(elem);
});
const a = stringCache.get('a');
expect(a).toEqual('1');
type tests = [Expect<Equal<typeof a, string | undefined>>];
});
The most important part is to understand how clone
function was typed.
export interface Cache<T> {
get: (key: string) => T | undefined;
set: (key: string, value: T) => void;
clone: <U>(transform: (elem: T) => U) => Cache<U>;
}
So clone
function take an transform
function as input arguement, and the clone
function itself returns a Cache
verson of the inner transform's
return type.
This is the way to do it:
clone: <U>(transform: (elem: T) => U) => Cache<U>;
You CANNOT put U
inside transform function:
clone: (transform: <U>(elem: T) => U) => Cache<U>; // does NOT work, U won't be available to Cache if it is defined in transform function
And good thing about this generic function, U
type will be auto infer from the return type of transform function.
it('Should let you clone the cache using a transform function', () => {
const numberCache = createCache<number>();
numberCache.set('a', 1);
numberCache.set('b', 2);
const stringCache = numberCache.clone((elem) => {
return String(elem);
});
const a = stringCache.get('a');
expect(a).toEqual('1');
type tests = [Expect<Equal<typeof a, string | undefined>>];
});
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2019-01-29 [Algorithom] Shuffle an array
2019-01-29 [TypeScript] Work with DOM Elements in TypeScript using Type Assertions
2019-01-29 [Tools] Unlock TypeScript's Features in Vanilla JS with @ts-check and JSDoc
2019-01-29 [Angular] Two things about OnChanges Lifecycle hook
2019-01-29 [Functional Programming 101] runWIth, evalWith, execWith
2019-01-29 [Functional Programming Monad] Modify The State Of A State Monad
2019-01-29 [Functional Programming Moand] Update The State Of A State Monad (put)