2016-04-09 6 views
1

Я переношу свой проект React от Babel до TypeScript. Похоже, что каждый раз я import что-то TypeScript кричит на меня:Требуется ли для каждого импорта в TypeScript файл * .d.ts?

error TS2307: Cannot find module './some-file'.

это как для внешних модулей (через npm) или местные другие файлы в моем проекте (что еще TypeScript не были yfied)

Есть ли способ сообщить TypeScript компилятор для обработки всего, что я импортирую как any, поэтому мне не нужно давать определения для них?

+0

вам нужно включить определенно типизированный файл машинописного текста для внешнего модуля. Файл типизации (.d.ts) для большей части общей библиотеки доступен по адресу https://github.com/DefinitelyTyped/DefinitelyTyped. Вам нужно включить этот файл .d.ts в свою папку с образцами и использовать их с помощью /// Ajay

+0

@Ajay не каждый модуль имеет определенно типизированные определения, также мне кажется, что 'd.ts 'определения для локальных файлов проекта, я ищу способы избежать этого –

+1

Я рекомендую жить с« ошибками »до тех пор, пока вы не закончите процесс типизации, иначе вы будете объявлять определения, которые станут устаревшими. Убедитесь, что вы отключили noImplicityAny. Вы все равно получите js-выход. –

ответ

0

ДА.

Вы можете объявить модули самостоятельно, но это может быть трудно управлять имена модулей при использовании относительных путей:

declare module "ol/OpenLayers-combined" { 
    var ol: any; 
    export = ol; 
} 

Здесь текущий день TSC вариант:

Version 1.8.7 
Syntax: tsc [options] [file ...] 

Examples: tsc hello.ts 
      tsc --out file.js file.ts 
      tsc @args.txt 

Options: 
--allowJs       Allow javascript files to be compiled. 
--allowSyntheticDefaultImports  Allow default imports from modules with no default export. This does not affect code emit, just typechecking. 
--allowUnreachableCode    Do not report errors on unreachable code. 
--allowUnusedLabels     Do not report errors on unused labels. 
-d, --declaration     Generates corresponding '.d.ts' file. 
--experimentalDecorators   Enables experimental support for ES7 decorators. 
--forceConsistentCasingInFileNames Disallow inconsistently-cased references to the same file. 
-h, --help       Print this message. 
--init        Initializes a TypeScript project and creates a tsconfig.json file. 
--jsx KIND       Specify JSX code generation: 'preserve' or 'react' 
--mapRoot LOCATION     Specifies the location where debugger should locate map files instead of generated locations. 
-m KIND, --module KIND    Specify module code generation: 'commonjs', 'amd', 'system', 'umd' or 'es2015' 
--moduleResolution     Specifies module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). 
--newLine NEWLINE     Specifies the end of line sequence to be used when emitting files: 'CRLF' (dos) or 'LF' (unix). 
--noEmit       Do not emit outputs. 
--noEmitOnError      Do not emit outputs if any errors were reported. 
--noFallthroughCasesInSwitch  Report errors for fallthrough cases in switch statement. 
--noImplicitAny      Raise error on expressions and declarations with an implied 'any' type. 
--noImplicitReturns     Report error when not all code paths in function return a value. 
--noImplicitUseStrict    Do not emit 'use strict' directives in module output. 
--outDir DIRECTORY     Redirect output structure to the directory. 
--outFile FILE      Concatenate and emit output to single file. 
--preserveConstEnums    Do not erase const enum declarations in generated code. 
--pretty KIND      Stylize errors and messages using color and context. (experimental) 
-p DIRECTORY, --project DIRECTORY Compile the project in the given directory. 
--reactNamespace     Specifies the object invoked for createElement and __spread when targeting 'react' JSX emit 
--removeComments     Do not emit comments to output. 
--rootDir LOCATION     Specifies the root directory of input files. Use to control the output directory structure with --outDir. 
--sourceMap       Generates corresponding '.map' file. 
--sourceRoot LOCATION    Specifies the location where debugger should locate TypeScript files instead of source locations. 
--suppressImplicitAnyIndexErrors Suppress noImplicitAny errors for indexing objects lacking index signatures. 
-t VERSION, --target VERSION  Specify ECMAScript target version: 'ES3' (default), 'ES5', or 'ES2015' (experimental) 
-v, --version      Print the compiler's version. 
-w, --watch       Watch input files. 
@<file>        Insert command line options and files from a file. 

Обратите внимание на noEmitOnError опции. Если для этого не установлено значение true, то даже недействительный машинописный текст должен скомпилироваться в javascript.

+0

Я ищу способ не объявлять тип для каждого импортированного модуля –

+0

Что вы просите, чтобы компилятор не предупреждал вас, когда вы сделали ссылку на недопустимый модуль. Пока вы не объявите модуль, вы увидите предупреждение (на самом деле нет ошибок TS). Вы должны полностью избегать оператора импорта и использовать raw require. –

Смежные вопросы