Code Monkey home page Code Monkey logo

Comments (8)

maverick1872 avatar maverick1872 commented on May 5, 2024 4

@kamilmysliwiec I'm struggling to get automatic return types to function. Could it have been possible that the lack of return type inference is related to #956?

Would love to drop the constant { infer: true } on every call if possible.

from config.

kamilmysliwiec avatar kamilmysliwiec commented on May 5, 2024 1

Makes total sense. Let me mark this issue as blocked for now. We'll wait till TS +4.1 is released

from config.

kamilmysliwiec avatar kamilmysliwiec commented on May 5, 2024 1

You can use [email protected] in the PR. We'll wait some time to make sure it's stable and then, publish a new major version of the package (to avoid introducing breaking changes in minor/patch releases).

from config.

kamilmysliwiec avatar kamilmysliwiec commented on May 5, 2024

Would you like to create a PR for this feature?

from config.

GavinRay97 avatar GavinRay97 commented on May 5, 2024

Hey @kamilmysliwiec, I was looking at the implementation in the repo and I realized that it's possible to pass a dot-path, if you're loading config from a custom provider and not just process.env.

Typing dot-paths will be possible in TS +4.1 with this PR:

microsoft/TypeScript#40336

type PropType<T, Path extends string> =
    string extends Path ? unknown :
    Path extends keyof T ? T[Path] :
    Path extends `${infer K}.${infer R}` ? K extends keyof T ? PropType<T[K], R> : unknown :
    unknown;

declare function getPropValue<T, P extends string>(obj: T, path: P): PropType<T, P>;
declare const s: string;

const obj = { a: { b: {c: 42, d: 'hello' }}};
getPropValue(obj, 'a');  // { b: {c: number, d: string } }
getPropValue(obj, 'a.b');  // {c: number, d: string }
getPropValue(obj, 'a.b.d');  // string
getPropValue(obj, 'a.b.x');  // unknown
getPropValue(obj, s);  // unknown

But currently, I think the limitations of TS are that the types for nested access can only be inferred if the argument is either an array or multiple variables, IE:

get(obj, ['user', 'email'])
get(obj, 'user', 'email')

So this might have to wait a bit unfortunately =/

from config.

kamilmysliwiec avatar kamilmysliwiec commented on May 5, 2024

@GavinRay97 TS4 is already available. Would you like to take a look at this?

from config.

GavinRay97 avatar GavinRay97 commented on May 5, 2024

Yeah I believe it should be possible to implement this now, due to:
https://devblogs.microsoft.com/typescript/announcing-typescript-4-1-beta/

I think the only requirement would be to set TS version to 4.1-beta and then later 4.1 in package.json to ensure the type doesn't break for other people, right?

from config.

kamilmysliwiec avatar kamilmysliwiec commented on May 5, 2024

Let's track this here #492

from config.

Related Issues (20)

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.