typtscript 组件中定义方法报错
不能将类型“{ onRef: (ref: any) => void; }”分配给类型“IntrinsicAttributes & IntrinsicClassAttributes<Component<Omit<IProps, “form”>, any, any>> & Readonly<…> & Readonly<…>”。
类型“IntrinsicAttributes & IntrinsicClassAttributes<Component<Omit<IProps, “form”>, any, any>> & Readonly<…> & Readonly<…>”上不存在属性“onRef”。
父组件中:
子组件中按照图片规范方法即可
onRef:(ref: any) => void;
无标红报错
报错事例typtscript 组件中定义方法报错不能将类型“{ onRef: (ref: any) => void; }”分配给类型“IntrinsicAttributes & IntrinsicClassAttributes<Component<Omit<IProps, “form”>, any, any>> & Readonly<…> & Readonly<…>”。类型“IntrinsicAttributes
const BuildTableRequlate:React.FC<any> = () => {
const content = ()=> (<div>一段文本</div>)
return content;
解决
方法
修改代码:
const BuildTableRequlate:React.FC<any> = () => {
const content =
注意:此文并不是把vue改为全部替换为
ts
,而是可以在原来的项目
中
植入
ts
文件,目前只是实践阶段,向
ts
转化过程
中
的过渡。
ts
有什么用?
类型检查、直接编译到原生js、引入新的语法糖
为什么用
ts
?
TypeScript的设计目的应该是解决JavaScript的“痛点”:弱类型和没有命名空间,导致很难模块化,不适合开发大型程序。另外它还提供了一些语法糖来帮助大家更方便地实践面向对象的编程。
typescript不仅可以约束我们的编码习惯,还能起到注释的作用,当我们看到一函数后我们立马就能知道这个函数的用法,需要传什么值,返回值是什么类型一目了然,对大型项目的维护性有很大的提升。也不至于使开发
python版本和ssl版本都会导致 reques
ts
在请求https网站时候会出一些
错
误,最好使用新版本。
1 Python2.6x use reques
ts
一台老Centos机器上跑着古老的应用,加了一个新模块之后
报
错
报
错
InsecurePlatformWarning: A true SSLContext object is not available.
/usr/lib/python2.6/site-packages/reques
ts
/packages/urllib3/util/ssl_.py:132: InsecurePlatformWarning: A true SSLCont
function doSome(x: number | string) {
if (typeof x === 'string') {
// 在这个块
中
,TypeScript 知道 `x` 的类型必须是 `string`
console.log(x.subtr(1)); // Error: 'subtr'
方法
并没有存在于 `string` 上
console.log(x.substr(1)); // ok
报
错
:
不能将类型“({ path: string; component: (props: RouteComponentProps<{}, StaticContext, unknown>) => Element; } | undefined)[]”分配给类型“RouteType[]”。
不能将类型“{ path: string; component: (props: RouteComponentProps<{}, StaticContext, unknown>) => E
在 TypeScript
中
,有几种
方法
可以避免使用 `any` 类型:
1. 使用类型断言(type assertion):在变量名前加上 `<类型>` 即可。例如:`let str: any = 'hello'; let strLength: number = (<string>str).length;`
2. 使用类型守卫(type guard):使用 `typeof` 或 `instanceof` 判断变量的类型,然后再进行相应的处理。例如:
function isString(value: any): value is string {
return typeof value === 'string';
let value: any = 'hello';
if (isString(value)) {
let strLength: number = value.length;
3. 使用联合类型(union type):将多种类型用 `|` 符号连接起来,表示变量的类型可以是其
中
任意一种。例如:`let value: string | number = 'hello';`
4. 使用字面量类型(literal type):可以指定变量只能是某几个特定的值。例如:`let direction: 'north' | 'south' | 'east' | 'west' = 'north';`
以上
方法
都可以帮助你避免使用 `any` 类型,同时也能提高程序的类型安全性。