medikoo / type

Runtime validation and processing of JavaScript types
ISC License
35 stars 4 forks source link

feat: add types declaration #6

Closed borracciaBlu closed 1 year ago

borracciaBlu commented 1 year ago

Hi @medikoo pls have a look and let me know.

to test this i've use the npm run test type in DefinitelyTyped to check the signatures.

To test the types in a project:

# test in an angular app 
npm install -g @angular/cli
ng new my-app
npm i https://github.com/borracciaBlu/type#feat/add-types

Then within the code you may have:

import ensure from 'type/ensure';
import ensureString from 'type/string/ensure';
import ensureNaturalNumber from 'type/natural-number/ensure';

var [repoName, issueNumber] = ensure(
      ["repoName", 1, ensureString],
      ["issueNumber", 3, ensureNaturalNumber],
      { Error: {message: "test"} }
);

To debug ts resolution:

./node_modules/.bin/tsc --noEmit --traceResolution

Let me know if you want me to add some example within the README.md.

codecov[bot] commented 1 year ago

Codecov Report

Merging #6 (92f3f70) into main (2947b1e) will not change coverage. The diff coverage is n/a.

:exclamation: Current head 92f3f70 differs from pull request most recent head b1aec60. Consider uploading reports for the commit b1aec60 to get more accurate results

@@           Coverage Diff           @@
##             main       #6   +/-   ##
=======================================
  Coverage   95.06%   95.06%           
=======================================
  Files          56       56           
  Lines         588      588           
=======================================
  Hits          559      559           
  Misses         29       29           

Help us with your feedback. Take ten seconds to tell us how you rate us.

borracciaBlu commented 1 year ago

Hi @medikoo ,

yes, it is possible using typesVersions.

   // for ts version higher than 3.1 use the declaration within the  ts-types folder
  "typesVersions": {
    ">=3.1": { "*": ["ts-types/*"] }  
  },

would you like me to use a separate folder instead of the current file disposition? if so, instead of ts-types would you like to use ts3.1 with reference to the ts version?

medikoo commented 1 year ago

would you like me to use a separate folder instead of the current file disposition?

@borracciaBlu yes, definitely, that sounds great.

One thing that is not clear to me is why we're referring to the v3.1 version? Is this the lowest TS version that supports the typesVersions filed?

Can't we just do it as follows?

 "typesVersions": {
    "*": { "*": ["ts-types/*"] }  
  },

Concerning folder name, ts-types feels best to me, but if you have a better idea, let me know. Still, I wouldn't include the TS version in the folder name (I assume there'll never be the case to host different typings for different TS versions - I would definitely want to avoid that)

borracciaBlu commented 1 year ago

Yes, that was because typesVersion was introduced in version 3.1.

I've put the * notation anyway, older version of ts will ignore it.

borracciaBlu commented 1 year ago

@medikoo the rest should be fine, except:

Let me know for the rest if it's ok pls.