这篇文章主要围绕[TypeScript]UseTypeScript’sneverTypeforExhaustivenessChecking展开,旨在为您提供一份详细的参考资料。我们将全面介绍[Type
这篇文章主要围绕[TypeScript] Use TypeScript’s never Type for Exhaustiveness Checking展开,旨在为您提供一份详细的参考资料。我们将全面介绍[TypeScript] Use TypeScript’s never Type for Exhaustiveness Checking,同时也会为您带来angular – 由typescript导入的代码的Typescript类型?、C# 和 TypeScript 之父宣布最新开源项目:TypeChat、Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias ''GeneratorConfig''. Cause:、Jest、TypeORM 和 typescript 无法运行测试的实用方法。
本文目录一览:- [TypeScript] Use TypeScript’s never Type for Exhaustiveness Checking
- angular – 由typescript导入的代码的Typescript类型?
- C# 和 TypeScript 之父宣布最新开源项目:TypeChat
- Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias ''GeneratorConfig''. Cause:
- Jest、TypeORM 和 typescript 无法运行测试
[TypeScript] Use TypeScript’s never Type for Exhaustiveness Checking
TypeScript 2.0 introduced a new primitive type called never
, the type of values that never occur. It helps model the completion behavior of functions more accurately and can also be used for exhaustiveness checking.
never type means that ''That part of code cannot be reached''.
In somecases, never type can be useful as well.
const enum ShirtSize {
XS,
S,
M,
L,
XL
}
function assertNever(value: never): never {
// throw Error(`Unexpected value ''${value}''`)
// Adjusted for plunker output
console.log(Error(`Unexpected value ''${value}''`));
}
function prettyPrint(size: ShirtSize) {
switch (size) {
case ShirtSize.S: console.log("small");
case ShirtSize.M: return "medium";
case ShirtSize.L: return "large";
case ShirtSize.XL: return "extra large";
// [ts] Argument of type ''ShirtSize.XS'' is
// not assignable to parameter of type ''never''.
default: return assertNever(size);
}
}
prettyPrint(ShirtSize.S)
prettyPrint(ShirtSize.XXL)
In the example, we want to make sure that every time in the enum ShirtSzie has been handled by the ''prettyPrint'' function.
But sometime, we might miss one case. That''s where ''assertNever'' function can help to make sure, we have gone though all the cases.
angular – 由typescript导入的代码的Typescript类型?
我正在开发一个Angular 7应用程序,我正在使用一些库,这些库是通过前端的脚本标签从其他来源(Stripe,recaptcha,googletags等)导入的.我将在这里使用Stripe作为示例,因为Stripe绝对要求客户端从Stripe.js导入前端库以实现PCI兼容性,因此从源服务器端不是一个选项.
我已经从DefinitelyTyped安装了Stripe的类型.如果我添加声明让Stripe:any;并且在组件中使用Stripe,它可以工作,但当然它不是类型安全的.如果我省略声明,VSCode发现类型很好,编辑器捕获类型错误,但它不会编译,(错误TS2304:找不到名称’Stripe’.)可能因为我没有Stripe的import语句.
有没有办法提示TypeScript它应该使用DefinitelyTyped中的类型来捕获类型错误,而不导入Stripe库本身?
编辑:我收回我所说的关于不认为这是Angular特定的内容.我现在认为这可能与角度编译过程有关.
解决方法
所以,我所做的是,我将Type DeFinition文件(通常是index.d.ts)复制到我的代码库中.
之后,您可以执行以下操作:
import {SomeType} from "sometype.d.ts" declare let ObjectImportedFromHTMLScriptTag: SomeType;
然后,您可以使用导入的对象而不会破坏类型安全性.
C# 和 TypeScript 之父宣布最新开源项目:TypeChat
C# 和 TypeScript 之父 Anders Hejlsberg 今天宣布了全新的开源项目 ——TypeChat,它通过 AI 在自然语言和应用程序模式 (application schema),以及 API 之间构建了一座 “桥梁”。
简单来说,TypeChat 是一个可以用 “人话” 来构建类型安全 UI 的工具库。
在过去的几个月里,我们已经见证了围绕大语言模型掀起的讨论热潮。
很多人以对话的方式与大模型进行交流,这也是最直接的方式。但这种方式面临一个问题:如何用最佳的方式将这些模型集成到现有的应用程序界面中。
换句话说,如何用自然语言接口增强传统 UI?如何使用 AI 来接受用户请求并将其转化为应用程序可以运行的内容?如何确保应用程序是安全的,并且所做的工作是开发者和用户都可以信任的?
TypeChat 库尝试解决上面这些问题,它使用代码库中的类型定义来确保结构化的 AI 响应是类型安全的。
使用示例
用户要求将这句话「我可以要一份蓝莓松饼和一杯特级拿铁咖啡吗?(Could I get a blueberry muffin and a grande latte?)」转换成 JSON,并只用 JSON 格式进行回复:
上面的示例并没指定类型,当指定类型后,TypeChat 的回复如下:
TypeChat 支持通过多种不同的方式使用。目前微软讨论的方式主要是使用数据模式将用户意图转换为结构化响应,事实上 TypeChat 还可以使用 API 模式来构建基本程序。
Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias ''GeneratorConfig''. Cause:
Caused by: org.apache.ibatis.builder.BuilderException: Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias ''GeneratorConfig''. Cause: java.lang.ClassNotFoundException: Cannot find class: GeneratorConfig
at org.apache.ibatis.builder.BaseBuilder.resolveClass(BaseBuilder.java:118)
at org.apache.ibatis.builder.xml.XMLMapperBuilder.resultMapElement(XMLMapperBuilder.java:265)
at org.apache.ibatis.builder.xml.XMLMapperBuilder.resultMapElement(XMLMapperBuilder.java:252)
at org.apache.ibatis.builder.xml.XMLMapperBuilder.resultMapElements(XMLMapperBuilder.java:244)
at org.apache.ibatis.builder.xml.XMLMapperBuilder.configurationElement(XMLMapperBuilder.java:116)
... 131 common frames omitted
Caused by: org.apache.ibatis.type.TypeException: Could not resolve type alias ''GeneratorConfig''. Cause: java.lang.ClassNotFoundException: Cannot find class: GeneratorConfig
at org.apache.ibatis.type.TypeAliasRegistry.resolveAlias(TypeAliasRegistry.java:120)
at org.apache.ibatis.builder.BaseBuilder.resolveAlias(BaseBuilder.java:149)
at org.apache.ibatis.builder.BaseBuilder.resolveClass(BaseBuilder.java:116)
... 135 common frames omitted
Caused by: java.lang.ClassNotFoundException: Cannot find class: GeneratorConfig
at org.apache.ibatis.io.ClassLoaderWrapper.classForName(ClassLoaderWrapper.java:200)
at org.apache.ibatis.io.ClassLoaderWrapper.classForName(ClassLoaderWrapper.java:89)
at org.apache.ibatis.io.Resources.classForName(Resources.java:261)
at org.apache.ibatis.type.TypeAliasRegistry.resolveAlias(TypeAliasRegistry.java:116)
... 137 common frames omitted
Jest、TypeORM 和 typescript 无法运行测试
如何解决Jest、TypeORM 和 typescript 无法运行测试?
我正在使用带有 TypeORM、MysqL 和 jest 的 TS 来构建模式工厂,但是,我的测试套件无法运行,因为它们抛出了有关导入语句的语法错误。我感到困惑的主要部分是项目构建并进入 dist/
文件夹,但是,运行如下所示的测试套件会引发错误。任何帮助将不胜感激,因为我不太了解问题所在。
错误被抛出
/Users/xxxx/Work/schemasproject/src/entity/HistoricalRental.ts:1
import { Entity } from ''typeorm'';
^^^^^^
SyntaxError: Cannot use import statement outside a module
at Object.compileFunction (node:vm:353:18)
at wrapSafe (node:internal/modules/cjs/loader:1039:15)
at Module._compile (node:internal/modules/cjs/loader:1073:27)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1138:10)
at Module.load (node:internal/modules/cjs/loader:989:32)
at Function.Module._load (node:internal/modules/cjs/loader:829:14)
at Module.require (node:internal/modules/cjs/loader:1013:19)
at require (node:internal/modules/cjs/helpers:93:18)
at /Users/xxxx/Work/schemasproject/node_modules/typeorm/util/DirectoryExportedClassesLoader.js:42:39
at Array.map (<anonymous>)
index.test.ts
describe(''Historical Sales Tests'',() => {
beforeAll(async () => {
console.log(''before'');
});
describe(''Creation'',() => {
it(''Should create the row'',async () => {
console.log(''here'');
});
});
});
/src/entity/HistoricalRental.ts - 其中 Property 是具有主键和一些列的基本类
import { Entity } from ''typeorm'';
import { Property } from ''../abstract/Property'';
@Entity()
export class HistoricalRental extends Property {}
package.json
{
"name": "schemas","version": "1.0.0","description": "","main": "src/index.ts","scripts": {
"build": "rm -rf dist && tsc","lint": "eslint ''./src/**/*.{ts,tsx}''","postpublish": "git clean -fd","prepublishOnly": "npm run build && cp -r ./dist/* . && rm -rf ./dist","pretest": "npm run lint","test": "jest --config=jest.config.js -i --forceExit","testOnly": "jest --config=jest.config.js -i --forceExit"
},"author": "","license": "ISC","devDependencies": {
"@types/jest": "^26.0.24","@typescript-eslint/eslint-plugin": "^4.28.5","@typescript-eslint/parser": "^4.28.5","eslint": "^7.31.0","eslint-config-airbnb-base": "^14.2.1","eslint-plugin-import": "^2.23.4","jest": "^27.0.6","ts-jest": "^27.0.4","typescript": "^4.3.5"
},"dependencies": {
"MysqL": "^2.18.1","typeorm": "^0.2.34"
}
}
jest.config.js
module.exports = {
testEnvironment: ''node'',roots: [''<rootDir>/src''],globalSetup: ''./src/__tests__/environment/globalSetup.ts'',globalTeardown: ''./src/__tests__/environment/globalTeardown.ts'',clearMocks: true,maxWorkers: 1,preset: ''ts-jest'',transform: {
''^.+\\.(ts|tsx)?$'': ''ts-jest'',},testRegex: ''(\\.|/)(test|spec)\\.ts?$'',modulefileExtensions: [''ts'',''tsx'',''js'',''jsx'',''json'',''node''],collectCoverage: true,coverageReporters: [''text''],};
tsconfig.json
{
"compilerOptions": {
"experimentalDecorators": true,"target": "ES6","module": "commonjs","declaration": true,"outDir": "./dist","rootDir": "./src","strict": true,"noImplicitAny": true,"strictnullchecks": true,"strictFunctionTypes": true,"strictPropertyInitialization": false,"noImplicitThis": true,"noUnusedLocals": true,"noUnusedParameters": true,"noImplicitReturns": true,"esModuleInterop": true,"forceConsistentCasingInFileNames": true,"emitDeclarationOnly": true
},"exclude": ["./src/__tests__","./src/__mocks__","./src/**/*.spec.ts","./dist/"]
}
ormconfig.js
const { join } = require(''path'');
module.exports = {
type: ''MysqL'',host: ''localhost'',port: 3306,username: ''test'',password: ''test'',database: ''test'',synchronize: true,logging: false,entities: [join(__dirname,''./**/*.entity{.ts,.js}'')],migrations: [
''src/migration/**/*.ts'',],subscribers: [
''src/subscriber/**/*.ts'',cli: {
entitiesDir: ''src/entity'',migrationsDir: ''src/migration'',subscribersDir: ''src/subscriber'',};
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)
关于[TypeScript] Use TypeScript’s never Type for Exhaustiveness Checking的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于angular – 由typescript导入的代码的Typescript类型?、C# 和 TypeScript 之父宣布最新开源项目:TypeChat、Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias ''GeneratorConfig''. Cause:、Jest、TypeORM 和 typescript 无法运行测试等相关知识的信息别忘了在本站进行查找喔。
本文标签: