Coder Social home page Coder Social logo

emmely2008 / typescriptchallange Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 6 KB

The requirements of the given assignment, which involves extending a get function to support dot notation paths and provide key suggestions. The return type of get method should be calculated correctly based on the provided path.

TypeScript 100.00%

typescriptchallange's Introduction

The Challange

// Extend this type to make it support the usage below.
// path should be recieved in dot notation and should give suggestions on keys.
// correct return type should be calculated depending on the given path.
declare function get(obj: {}, path: string): unknown;

const object = {
    "user": {
        "fullName": "Janis Pagac",
        "age": 30,
        "address": {
            "street": "83727 Beatty Garden",
            "city": "Hamilton"
        },
        "friends": {
            "0": {
                "fullName": "Franklin Kuhn",
                "age": 20
            },
            "1": {
                "fullName": "Hubert Sawayn"
            }
        }
    }
} ;

const fullName = get(object, "user.fullName");
const age = get(object, "user.age");
const street = get(object, "user.address.street");
const fullNameOfFriend = get(object, "user.friends.0.fullName");
const ageOfFriend = get(object, "user.friends.0.age");

The solution

In my solution, found in index.ts, I first defined the User type by combining the properties of the Person interface with additional properties like address and friends using key mappings. This ensures that the User type includes all the necessary properties and avoiding redundancy.

Next, I introduced the getPossibleKeys type, which generates valid path keys as strings based on the object type. This type handles arrays by using conditional types to recursively generate keys for array elements. This allows for proper suggestions on keys when using dot notation.

The valid path keys for the object in the example is:

"fullName" "age" "friends.0.fullName" "friends.0.age" "address.city" "address.street"

To extract the correct return type based on the given path, I defined the GetValue type. This type traverses the object based on the keys in the path, checking for object and array types along the way. The return type is determined by the final key in the path.

Why I dropped the string user

When I dropped the "user" part in the paths like get(object, "user.fullName"), it was because the object itself was already an instance of the User type. Since the User type includes all the properties defined in the Person interface, as well as the address and friends properties, I could directly access the properties of object without specifying the "user" property in the path.

So instead of using get(object, "user.fullName"), I could simply use get(object, "fullName") because the fullName property is already part of the User type. The same applied to other properties like age, address.street, and so on. They were all directly accessible on the object of type User.

By defining const object: User = { ... }, I created an instance of the User type that included all the required properties defined in the User type itself, as well as the properties inherited from the Person interface.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.