gpt4 book ai didi

javascript - 无法使用 --out Comment : Getting Error 将 typescript 模块 (.ts) 文件连接到单个输出 (.js)

转载 作者:行者123 更新时间:2023-12-02 16:06:36 25 4
gpt4 key购买 nike

我是 Typescript 新手。我一直在尝试使用 tsc --out 命令来连接 typescript 模块。但我收到一些错误。没有有关该错误的信息。下面是过程,我累了。

我有.ts文件:

验证.ts:

module Validation {  
export interface StringValidator {
isAcceptable(s: string): boolean;
}
}

ZipCodeValidator.ts:

/// <reference path="Validation.ts" />
module Validation {
var numberRegexp = /^[0‐9]+$/;
export class ZipCodeValidator implements StringValidator {
isAcceptable(s: string) {
return s.length === 5 && numberRegexp.test(s);
}
}
}

LettersOnlyValidator.ts:

/// <reference path="Validation.ts" />
module Validation {
var lettersRegexp = /^[A‐Za‐z]+$/;
export class LettersOnlyValidator implements StringValidator {
isAcceptable(s: string) {
return lettersRegexp.test(s);
}
}
}

测试.ts:

/// <reference path="Validation.ts" />
/// <reference path="LettersOnlyValidator.ts" />
/// <reference path="ZipCodeValidator.ts" />
// Some samples to try
var strings = ['Hello', '98052', '101'];
// Validators to use
var validators: { [s: string]: Validation.StringValidator; } = {};
validators['ZIP code'] = new Validation.ZipCodeValidator();
validators['Letters only'] = new Validation.LettersOnlyValidator();
// Show whether each string passed each validator
strings.forEach(s => {
for (var name in validators) {
console.log('"' + s + '" ' + (validators[name].isAcceptable(s) ? ' matches ' : ' does not match ') + name);
}
});

tsc 命令:

tsc --out sample.js Test.ts

错误:

error TS6053: File 'ΓÇÉout.ts' not found.  
error TS6054: File 'sample.js' must have extension '.ts' or '.d.ts'

请告诉我解决方法。还有一个问题,有没有办法在 gulp 中连接 typescript 模块?

最佳答案

我不会准确地回答你,因为我不使用 tsc 命令。

但如果我是你,我会让它自动化(例如使用 Gulp):

var gulp = require('gulp');
var typescript = require('gulp-typescript');
var sourcemaps = require('gulp-sourcemaps');
var addsrc = require('gulp-add-src');
var concat = require('gulp-concat');

gulp.task('tsc', function () {
return gulp.src(['*.ts']) // all your ts files here (check the path)
.pipe(sourcemaps.init())
.pipe(typescript({
sortOutput: true
}))
.js // compile with tsc, ordered with _reference.ts
.pipe(addsrc('external.js')) // add an external javascript file (if needed)
.pipe(concat('app.js')) // concat all in one file
.pipe(sourcemaps.write()) // generate the .map
.pipe(gulp.dest('dist')); // write all in the dist folder
});

简而言之:

关于javascript - 无法使用 --out Comment : Getting Error 将 typescript 模块 (.ts) 文件连接到单个输出 (.js),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30664972/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com