Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typing separator with an array instead of just string #157

Open
2 tasks done
jm42 opened this issue Jul 11, 2023 · 0 comments
Open
2 tasks done

Typing separator with an array instead of just string #157

jm42 opened this issue Jul 11, 2023 · 0 comments

Comments

@jm42
Copy link

jm42 commented Jul 11, 2023

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Fastify version

4.19.2

Plugin version

No response

Node.js version

20.x

Operating system

Linux

Operating system version (i.e. 20.04, 11.3, 10)

6.4.1

Description

Hello,

I would like to type ALLOWED_HOSTS as string[] instead of string.

interface ConfigEnv {
  ALLOWED_HOSTS: string
}

export function loadConfig(): ConfigEnv {
  return envSchema({
    schema: {
      type: 'object',
      required: [
        'ALLOWED_HOSTS',
      ],
      properties: {
        ALLOWED_HOSTS: {
          type: 'string',
          separator: ',',
        },
      },
    },
  })
}

Now the actual type of loadConfig().ALLOWED_HOSTS is an array, empty or not. Here the type is incorrect to be string and not string[]. But if I change it I get the error on the line of type: "string":

Type '"string"' is not assignable to type '"array"'.ts(2322)

How can I do? Thanks.

Steps to Reproduce

// a.js
import envSchema = require("env-schema")

interface ConfigEnv {
  ALLOWED_HOSTS: string[]
}

export function loadConfig(): ConfigEnv {
  return envSchema({
    schema: {
      type: 'object',
      required: [
        'ALLOWED_HOSTS',
      ],
      properties: {
        ALLOWED_HOSTS: {
          type: 'string',
          separator: ',',
        },
      },
    },
  })
}

// tsconfig.json
{
  "compilerOptions": {
    "target": "ES2020",
    "module": "commonjs",
    "lib": [
      "es2020",
      "dom"
    ],
    "declaration": true,
    "strict": true,
    "noImplicitAny": true,
    "strictNullChecks": true,
    "noImplicitThis": true,
    "alwaysStrict": true,
    "noUnusedLocals": false,
    "noUnusedParameters": false,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": false,
    "inlineSourceMap": true,
    "inlineSources": true,
    "experimentalDecorators": true,
    "strictPropertyInitialization": false,
    "typeRoots": [
      "./node_modules/@types"
    ],
    "outDir": "dist",
    "resolveJsonModule": true
  },
  "exclude": [
    "node_modules",
    "cdk.out",
    "dist"
  ]
}

Run:

$ ts-node a.ts

Error:

TSError: ⨯ Unable to compile TypeScript:
a.ts(16,11): error TS2322: Type '"string"' is not assignable to type '"array"'.

Expected Behavior

Type ALLOWED_HOSTS as:

interface ConfigEnv {
  ALLOWED_HOSTS: string[]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant