nice-grpc icon indicating copy to clipboard operation
nice-grpc copied to clipboard

bug in readme?

Open scippio opened this issue 2 years ago • 4 comments

Hello!

I tried generate example step-by-step with example codes and I get this error: SyntaxError: The requested module '/src/lib/proto/example.ts' does not provide an export named 'ExampleServiceClient' because ExampleServiceClient is generated only like interface...

So I think the right example from readme:

import {createChannel, createClient} from 'nice-grpc-web';
import {
  ExampleServiceClient,
  ExampleServiceDefinition,
} from './compiled_proto/example';

const channel = createChannel('http://localhost:8080');

const client: ExampleServiceClient = createClient(
  ExampleServiceDefinition,
  channel,
);

should be this:

import {createChannel, createClient} from 'nice-grpc-web';
import type { ExampleServiceClient } from './compiled_proto/example';
import { ExampleServiceDefinition } from './compiled_proto/example';

const channel = createChannel('http://localhost:8080');

const client: ExampleServiceClient = createClient(
  ExampleServiceDefinition,
  channel,
);

scippio avatar Sep 05 '23 11:09 scippio

Hey,

This must have something to do with your tooling. With vanilla tsc you should be allowed to import types using regular import (not just import type).

I'm fine with making the proposed change to the docs, but I'd like to figure it out first.

Which bundler are you using?

aikoven avatar Sep 11 '23 08:09 aikoven

I opened the tsconfig.json and I maybe found the reason (in comment) :grimacing: :

{
	"compilerOptions": {
		"moduleResolution": "node",
		"module": "es2020",
		"lib": ["es2020"],
		"target": "es2019",
		/**
			svelte-preprocess cannot figure out whether you have a value or a type, so tell TypeScript
			to enforce using \`import type\` instead of \`import\` for Types.
			*/
....

scippio avatar Sep 12 '23 18:09 scippio

So what triggers this error is this setting:

"importsNotUsedAsValues": "error"

Thanks for the report! I will fix the readme.

aikoven avatar Sep 13 '23 06:09 aikoven

This is my full tsconfig.json:

{
	"compilerOptions": {
		"moduleResolution": "node",
		"module": "es2020",
		"lib": ["es2020"],
		"target": "es2019",
		/**
			svelte-preprocess cannot figure out whether you have a value or a type, so tell TypeScript
			to enforce using \`import type\` instead of \`import\` for Types.
			*/
		// "importsNotUsedAsValues": "error",
		// "verbatimModuleSyntax": true,
		"isolatedModules": true,
		"resolveJsonModule": true,
		/**
			To have warnings/errors of the Svelte compiler at the correct position,
			enable source maps by default.
			*/
		"sourceMap": true,
		"esModuleInterop": true,
		"skipLibCheck": true,
		"forceConsistentCasingInFileNames": true,
		"baseUrl": ".",
		"allowJs": true,
		"checkJs": true,
		"paths": {
			"$lib":["src/lib"],
			"$lib/*":["src/lib/*"]
		}
	},
	"include": ["src/*.d.ts", "src/**/*.d.ts", "src/**/*.js", "src/**/*.ts", "src/**/*.svelte"],
	"extends": "./.svelte-kit/tsconfig.json"
}

importsNotUsedAsValues is deprecated: https://www.typescriptlang.org/tsconfig#importsNotUsedAsValues so I think it's ok when I have it commented. So I think this problem is here because I'm using Svelte: svelte-preprocess ...

scippio avatar Sep 13 '23 09:09 scippio