前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >typescript-exercises(十五)

typescript-exercises(十五)

作者头像
阿超
发布2024-11-11 09:16:59
发布2024-11-11 09:16:59
9900
代码可运行
举报
文章被收录于专栏:快乐阿超快乐阿超
运行总次数:0
代码可运行

人的一辈子都在高潮—低潮中浮沉,唯有庸碌的人,生活才如死水一般。——傅雷

问题:

代码语言:javascript
代码运行次数:0
复制
/*

Intro:

    Our attempt to Open Source didn't work quite as
    expected. It turned out there were already many
    existing functional JS libraries.

    All the remaining developers left the company as
    well. It seems that they are joining a very
    ambitious startup which re-invented a juicer and
    raised millions of dollars.
    Too bad we cannot compete with this kind of
    financing even though we believe our idea is
    great.

    It's time to shine for the last time and publish
    our new invention: object-constructor as our CTO
    named it. A small library which helps
    manipulating an object.

Exercise:

    Here is a library which helps manipulating objects.
    We tried to write type annotations and we failed.
    Please help!

*/

export class ObjectManipulator {

    constructor(protected obj) {}

    public set(key, value) {
        return new ObjectManipulator({...this.obj, [key]: value});
    }

    public get(key) {
        return this.obj[key];
    }

    public delete(key) {
        const newObj = {...this.obj};
        delete newObj[key];
        return new ObjectManipulator(newObj);
    }

    public getObject() {
        return this.obj;
    }
}

报错:

代码语言:javascript
代码运行次数:0
复制
index.ts(32,17): error TS7006: Parameter 'obj' implicitly has an 'any' type.
index.ts(34,16): error TS7006: Parameter 'key' implicitly has an 'any' type.
index.ts(34,21): error TS7006: Parameter 'value' implicitly has an 'any' type.
index.ts(38,16): error TS7006: Parameter 'key' implicitly has an 'any' type.
index.ts(42,19): error TS7006: Parameter 'key' implicitly has an 'any' type.
test.ts(9,12): error TS2344: Type 'false' does not satisfy the constraint 'true'.
test.ts(19,12): error TS2344: Type 'false' does not satisfy the constraint 'true'.
test.ts(27,12): error TS2344: Type 'false' does not satisfy the constraint 'true'.
test.ts(33,12): error TS2344: Type 'false' does not satisfy the constraint 'true'.

答案:

代码语言:javascript
代码运行次数:0
复制
/*

Intro:

    Our attempt to Open Source didn't work quite as
    expected. It turned out there were already many
    existing functional JS libraries.

    All the remaining developers left the company as
    well. It seems that they are joining a very
    ambitious startup which re-invented a juicer and
    raised millions of dollars.
    Too bad we cannot compete with this kind of
    financing even though we believe our idea is
    great.

    It's time to shine for the last time and publish
    our new invention: object-constructor as our CTO
    named it. A small library which helps
    manipulating an object.

Exercise:

    Here is a library which helps manipulating objects.
    We tried to write type annotations and we failed.
    Please help!

*/

type ObjectWithNewProp<T, K extends string, V> = T & {[NK in K]: V};

export class ObjectManipulator<T> {
    constructor(protected obj: T) {}

    public set<K extends string, V>(key: K, value: V): ObjectManipulator<ObjectWithNewProp<T, K, V>> {
        return new ObjectManipulator({...this.obj, [key]: value} as ObjectWithNewProp<T, K, V>);
    }

    public get<K extends keyof T>(key: K): T[K] {
        return this.obj[key];
    }

    public delete<K extends keyof T>(key: K): ObjectManipulator<Omit<T, K>> {
        const newObj = {...this.obj};
        delete newObj[key];
        return new ObjectManipulator(newObj);
    }

    public getObject(): T {
        return this.obj;
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2024-11-10,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档