Typescript: How about using `typeRoots` instead of `types` in tsconfig.json?

Created on 22 Mar 2019  路  4Comments  路  Source: nuxt/typescript

What problem does this feature solve?

I think it is better to use typeRoots than types.
That's because if you use types, you have to add it to types explicitly when you add @types/xxx as your dependency.

What does the proposed changes look like?

Specifically, you should write the following in defaultTsJsonConfig:

export const defaultTsJsonConfig = {
  compilerOptions: {
    target: 'esnext',
    module: 'esnext',
    moduleResolution: 'node',
    lib: [
      'esnext',
      'esnext.asynciterable',
      'dom'
    ],
    esModuleInterop: true,
    experimentalDecorators: true,
    allowJs: true,
    sourceMap: true,
    strict: true,
    noImplicitAny: false,
    noEmit: true,
    baseUrl: '.',
    paths: {
      '~/*': [
        './*'
      ],
      '@/*': [
        './*'
      ]
    },
    typeRoots: [
      './node_modules/@types',
      './node_modules/@nuxt/vue-app/types'
    ]
  }
}
This feature request is available on Nuxt community (#c8889)
transfered

Most helpful comment

@iwata The issue with typeRoots is that it needs relative path, and people might have their node_modules not in current folder where is running nuxt.

See
https://www.typescriptlang.org/docs/handbook/tsconfig-json.html#types-typeroots-and-types

I suggest keeping types for now.

I'm working on changes which will only create tsconfig.json with these values only if the file is missing. So then people can change and use typeRoots (they already can do) if they really need it.

All 4 comments

@iwata Thanks for your proposal, I will closely check if it better fits for the next major TS change (which should switch back to the extends way instead of overriding tsconfig.json when Nuxt is starting)

@iwata The issue with typeRoots is that it needs relative path, and people might have their node_modules not in current folder where is running nuxt.

See
https://www.typescriptlang.org/docs/handbook/tsconfig-json.html#types-typeroots-and-types

I suggest keeping types for now.

I'm working on changes which will only create tsconfig.json with these values only if the file is missing. So then people can change and use typeRoots (they already can do) if they really need it.

@kevinmarrec
Thanks your prompt reply.

I'm working on changes which will only create tsconfig.json with these values only if the file is missing.

That's great.
It's related by this second question.
https://cmty.app/nuxt/nuxt.js/issues/c8891

@iwata Everything you mentioned won't be longer issues with https://github.com/nuxt/nuxt.js/pull/5367

And yes, @nuxt/typescript should be installed as dependency and not devDependency to prevent error in production to be able to use nuxt start. See docs PR linked to the link above, I changed it so it tells user to install it as dependency.

It will also be in released notes when it will be released.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

nandenjin picture nandenjin  路  4Comments

johanbaaij picture johanbaaij  路  3Comments

negezor picture negezor  路  6Comments

JakubKoralewski picture JakubKoralewski  路  6Comments

andrewvasilchuk picture andrewvasilchuk  路  4Comments