This repository was archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
fix: typedef resolution & add examples that use types #3359
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c455699
fix: typescirpt typedef resolution
Gozala 1dd7b61
addres review comments
Gozala 58fc329
Merge remote-tracking branch 'upstream/master' into gozala/fix-type-p…
Gozala 09a6bcc
chore: align versions
Gozala a6df558
chore: fix typos
achingbrain 84d5b70
chore: rename
achingbrain File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# Using IPFS with Typescript | ||
|
||
> This example provides a template for using js-ipfs in typescript project. | ||
|
||
|
||
## Before you start | ||
|
||
First clone this repo, install dependencies in the project root. | ||
|
||
``` | ||
git clone https://github.com/ipfs/js-ipfs.git | ||
cd js-ipfs/examples/types-use-ipfs-from-ts | ||
npm install | ||
``` | ||
|
||
You can type check this example by runing following in the example directory: | ||
|
||
``` | ||
npm test | ||
``` | ||
|
||
You should see following output: | ||
|
||
``` | ||
> tsc --noEmit | ||
``` | ||
|
||
If you remove `// @ts-expect-error` comment is `src/main.ts` on line 16 and run `npm test` once again you should see a following output instead: | ||
|
||
``` | ||
tsc --noEmit | ||
|
||
src/main.ts:16:14 - error TS2339: Property 'toUpperCase' does not exist on type 'CID'. | ||
|
||
16 file.cid.toUpperCase() | ||
~~~~~~~~~~~ | ||
|
||
|
||
Found 1 error. | ||
``` | ||
|
||
|
||
## IntelliSense | ||
|
||
In the vscode and other code editors that provide [comparable IntelliSense features](https://docs.microsoft.com/en-us/visualstudio/ide/using-intellisense?view=vs-2019) you should be able to get code auto complete, paramatere and return value information for `js-ipfs` APIs. | ||
|
||
 | ||
|
||
## Limitations | ||
|
||
- Things should work out of the box, with most `tsconfig` settings, however unless | ||
[`skipLibCheck`](https://www.typescriptlang.org/tsconfig#skipLibCheck) is set to `true` many errors will be reported. | ||
> That is because | ||
types are generated from source JSDoc comments and typescript seems to emit declarations which it then complains about. | ||
|
||
- Not all APIs are fully entyped so you might observe gaps and `any` types here and there. We hope to improve this over time. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"name": "types-use-ipfs-from-ts", | ||
"private": true, | ||
"dependencies": { | ||
"ipfs": "^0.51.0" | ||
}, | ||
"devDependencies": { | ||
"typescript": "^4.0.3" | ||
}, | ||
"scripts": { | ||
"test": "tsc --noEmit" | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import IPFS from 'ipfs' | ||
|
||
export default async function main () { | ||
const node = await IPFS.create() | ||
const version = await node.version() | ||
|
||
console.log('Version:', version.version) | ||
|
||
const file = await node.add({ | ||
path: 'hello.txt', | ||
content: new TextEncoder().encode('Hello World 101') | ||
}) | ||
|
||
console.log('Added file:', file.path, file.cid.toString()) | ||
try { | ||
file.cid.toUpperCase() | ||
} catch(error) { | ||
|
||
} | ||
|
||
const decoder = new TextDecoder() | ||
let content = '' | ||
for await (const chunk of node.cat(file.cid)) { | ||
content += decoder.decode(chunk) | ||
} | ||
|
||
console.log('Added file contents:', content) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"compilerOptions": { | ||
"strict": true, | ||
"skipLibCheck": true, | ||
"noImplicitAny": false, | ||
"esModuleInterop": true, | ||
"moduleResolution": "Node", | ||
"noEmit": true | ||
}, | ||
"include": [ | ||
"src" | ||
] | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# Using IPFS with typed JS | ||
|
||
> This example provides a template for setting up a [JS project that utilizes type checker](https://www.typescriptlang.org/docs/handbook/type-checking-javascript-files.html) and `js-ipfs` type [declarations the were generated from JSDoc comments](https://www.typescriptlang.org/docs/handbook/declaration-files/dts-from-js.html) | ||
|
||
Things should work out of the box, only requirement is to disable | ||
[`skipLibCheck`](https://www.typescriptlang.org/tsconfig#skipLibCheck) because type declarations generated by typescript for commonjs modules raise some issues when consumed. | ||
|
||
|
||
## Before you start | ||
|
||
First clone this repo, install dependencies in the project root. | ||
|
||
``` | ||
git clone https://github.com/ipfs/js-ipfs.git | ||
cd js-ipfs/examples/types-use-ipfs-from-typed-js | ||
npm install | ||
``` | ||
|
||
## Type checking | ||
|
||
You can type check this example by runing following in the example directory: | ||
|
||
``` | ||
npm test | ||
``` | ||
|
||
You should see following output: | ||
|
||
``` | ||
> tsc --noEmit | ||
``` | ||
|
||
If you remove `// @ts-expect-error` comment is `src/main.js` on line 16 and run `npm test` once again you should see a following output instead: | ||
|
||
``` | ||
> tsc --noEmit | ||
src/main.js:16:14 - error TS2339: Property 'toUpperCase' does not exist on type 'CID'. | ||
|
||
16 file.cid.toUpperCase() | ||
~~~~~~~~~~~ | ||
|
||
|
||
Found 1 error. | ||
``` | ||
|
||
## IntelliSense | ||
|
||
In the vscode and other code editors that provide [comparable IntelliSense features](https://docs.microsoft.com/en-us/visualstudio/ide/using-intellisense?view=vs-2019) you should be able to get code auto complete, paramatere and return value information for `js-ipfs` APIs. | ||
achingbrain marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
 | ||
|
||
## Limitations | ||
|
||
- Things should work out of the box, with most `tsconfig` settings, however unless | ||
[`skipLibCheck`](https://www.typescriptlang.org/tsconfig#skipLibCheck) is set to `true` many errors will be reported. | ||
> That is because | ||
types are generated from source JSDoc comments and typescript seems to emit declarations which it then complains about. | ||
|
||
- Not all APIs are fully entyped so you might observe gaps and `any` types here and there. We hope to improve this over time. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"name": "types-use-ipfs-from-typed-js", | ||
"private": true, | ||
"dependencies": { | ||
"ipfs": "^0.51.0" | ||
}, | ||
"devDependencies": { | ||
"typescript": "^4.0.3" | ||
}, | ||
"scripts": { | ||
"test": "tsc --noEmit" | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
const IPFS = require('ipfs') | ||
|
||
async function main () { | ||
const node = await IPFS.create() | ||
const version = await node.version() | ||
|
||
console.log('Version:', version.version) | ||
|
||
const file = await node.add({ | ||
path: 'hello.txt', | ||
content: new TextEncoder().encode('Hello World 101') | ||
}) | ||
|
||
console.log('Added file:', file.path, file.cid.toString()) | ||
try { | ||
file.cid.toUpperCase() | ||
} catch(error) { | ||
|
||
} | ||
|
||
const decoder = new TextDecoder() | ||
let content = '' | ||
for await (const chunk of node.cat(file.cid)) { | ||
content += decoder.decode(chunk) | ||
} | ||
|
||
console.log('Added file contents:', content) | ||
} | ||
|
||
main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"compilerOptions": { | ||
"allowJs": true, | ||
"checkJs": true, | ||
"strict": true, | ||
"skipLibCheck": true, | ||
"noImplicitAny": false, | ||
"esModuleInterop": true, | ||
"moduleResolution": "Node", | ||
"noEmit": true | ||
}, | ||
"include": [ | ||
"src" | ||
] | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,8 @@ | |
"typesVersions": { | ||
"*": { | ||
"*": [ | ||
"dist/*" | ||
"dist/*", | ||
"dist/*/index" | ||
] | ||
} | ||
}, | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,7 +28,8 @@ | |
"typesVersions": { | ||
"*": { | ||
"*": [ | ||
"dist/*" | ||
"dist/*", | ||
"dist/*/index" | ||
] | ||
} | ||
}, | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.