sindresorhus/conf

Example from readme with typescript

kevinsimper opened this issue · 5 comments

I tried the example and it does not seem to compile

import Conf from 'conf'

const schema = {
	bar: {
		type: 'string'
	}
};

const config = new Conf({schema});

https://repl.it/repls/AliveLumberingTechnicians#index.ts

/usr/local/lib/node_modules/ts-node-fm/src/index.ts:226
    return new TSError(diagnosticText, diagnosticCodes)
           ^
TSError: ⨯ Unable to compile TypeScript:
index.ts:9:25 - error TS2345: Argument of type '{ schema: { bar: { type: string; }; }; }' is not assignable to parameter of type 'Readonly<Partial<Options<{ bar: {}; }>>>'.
  Types of property 'schema' are incompatible.
    Type '{ bar: { type: string; }; }' is not assignable to type 'Schema<{ bar: {}; }>'.
      Types of property 'bar' are incompatible.
        Type '{ type: string; }' is not assignable to type 'JSONSchema'.
          Types of property 'type' are incompatible.
            Type 'string' is not assignable to type '"string" | "number" | "boolean" | "object" | JSONSchemaType | "array" | "integer" | "null" | ("string" | "number" | "boolean" | "object" | JSONSchemaType | "array" | "integer" | "null")[] | undefined'.

9 const config = new Conf({schema});
                          ~~~~~~~~

    at createTSError (/usr/local/lib/node_modules/ts-node-fm/src/index.ts:226:12)
    at getOutput (/usr/local/lib/node_modules/ts-node-fm/src/index.ts:335:40)
    at Object.compile (/usr/local/lib/node_modules/ts-node-fm/src/index.ts:368:11)
    at startRepl (/usr/local/lib/node_modules/ts-node-fm/src/bin.ts:147:28)
    at Object.<anonymous> (/usr/local/lib/node_modules/ts-node-fm/src/bin.ts:66:1)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)

Can you reproduce this with the latest TypeScript version?

We have a type test for this:

new Conf<UnicornFoo>({
schema: {
foo: {
type: 'string',
default: 'foobar'
},
unicorn: {
type: 'boolean'
},
hello: {
type: 'number'
},
nested: {
type: 'object',
properties: {
prop: {
type: 'number'
}
}
}
}
});

@sindresorhus I clone the repo and it is appearently down to how you define the schema, I am really confused: #121

The same error here: https://travis-ci.com/github/sindresorhus/conf/jobs/370496506

Located the issue, we expected the schema to by typed like const schema: Schema<UnicornFoo>, but we should also allow standard objects for people who do not want to explicitly type it.

Some good solutions has come up 👍