8wDlpd.png
8wDFp9.png
8wDEOx.png
8wDMfH.png
8wDKte.png

为什么当将对象类型与字符串相交时,TypeScript 不会显示“从不输入”错误?

SingleNegationElimination 3月前

252 0

我试图理解为什么当对象类型与字符串类型相交时,TypeScript 没有在错误消息中明确显示 never 类型。这是我的代码:type Test1 = { b: numb...

我试图理解为什么当对象类型与字符串类型相交时,TypeScript 没有在错误消息中明确显示 never 类型。这是我的代码:

type Test1 = {
    b: number;
    c: string;
} & string;

// Type '{ b: number; c: string; }' is not assignable to type 'Test1'.
// Type '{ b: number; c: string; }' is not assignable to type 'string'.
let test1: Test1 = {
    b: 1,
    c: 'c',
}

// Type 'string' is not assignable to type 'Test1'.
// Type 'string' is not assignable to type '{ b: number; c: string; }'.
let test1: Test1 = 'test1'

在上面的代码中,Test1 被定义为对象类型和字符串的交集。根据我的理解,这应该导致 never 类型,因为没有值可以同时是具有属性的对象 b c 字符串。

  1. 当尝试将一个对象分配给 test1 时,我得到:
Type '{ b: number; c: string; }' is not assignable to type 'Test1'.
Type '{ b: number; c: string; }' is not assignable to type 'string'.
  1. 当尝试将字符串分配给 test1 时,我得到:
Type 'string' is not assignable to type 'Test1'.
Type 'string' is not assignable to type '{ b: number; c: string; }'.

据我了解,这些应该表明 Test1 实际上是 never 类型,但 TypeScript 似乎没有在错误消息中提到 never。这是 TypeScript 中的错误,还是我在 TypeScript 处理此类交集的方式上遗漏了什么?

如能就此行为做出任何澄清,我们将不胜感激。谢谢!

帖子版权声明 1、本帖标题:为什么当将对象类型与字符串相交时,TypeScript 不会显示“从不输入”错误?
    本站网址:http://xjnalaquan.com/
2、本网站的资源部分来源于网络,如有侵权,请联系站长进行删除处理。
3、会员发帖仅代表会员个人观点,并不代表本站赞同其观点和对其真实性负责。
4、本站一律禁止以任何方式发布或转载任何违法的相关信息,访客发现请向站长举报
5、站长邮箱:yeweds@126.com 除非注明,本帖由SingleNegationElimination在本站《typescript》版块原创发布, 转载请注明出处!
最新回复 (0)
  • 我在转换 numberMy 代码时收到 DecimalPipe 错误:Serviceexport interface RoomList { roomNumber: number; roomType: string; amenities: string; price: number; pho...

    我在转换数字时收到 DecimalPipe 错误

    我的代码: 服务

    export interface RoomList {
        roomNumber: number;
        roomType: string;
        amenities: string;
        price: number;
        photos: string;
        checkinTime: Date;
        checkoutTime: Date;
        rating: Number;
    }
    

    成分

    import { Component } from '@angular/core';
    import { Room, RoomList } from './rooms';
    import { CurrencyPipe, DatePipe, DecimalPipe, LowerCasePipe, NgClass, NgFor, NgIf, NgStyle } from '@angular/common';
    
    @Component({
      selector: 'app-rooms',
      standalone: true,
      imports: [NgIf, NgFor, NgClass, NgStyle, DatePipe, CurrencyPipe, LowerCasePipe, DecimalPipe],
      templateUrl: './rooms.component.html',
      styleUrl: './rooms.component.scss'
    })
    export class RoomsComponent {
      hotelName = "Hilton Hotel";
      numberOfRoom = 10;
      hideRoom = false;
      rooms: Room = {
        availableRooms: 10,
        bookedRooms: 5,
        totalRooms: 20
      };
    
      roomList: RoomList[] = [
        {
          roomNumber: 1,
          roomType: 'Heloe',
          amenities: "HN",
          price: 5000,
          photos: './image2',
          checkinTime: new Date('11-Nov-2021'),
          checkoutTime: new Date('12-Nov-2021'),
          rating: 4.555
        },
        {
          roomNumber: 2,
          roomType: 'Donh',
          amenities: "NB",
          price: 10000,
          photos: './image2',
          checkinTime: new Date('17-Sep-2021'),
          checkoutTime: new Date('20-Sep-2021'),
          rating: 3.516
        },
        {
          roomNumber: 3,
          roomType: 'Holds',
          amenities: "DN",
          price: 200,
          photos: './image3',
          checkinTime: new Date('17-Sep-2021'),
          checkoutTime: new Date('20-Sep-2021'),
          rating: 3.99999
        },
        {
          roomNumber: 4,
          roomType: 'Bed',
          amenities: "CT",
          price: 1000,
          photos: './image4',
          checkinTime: new Date('11-Mar-2024'),
          checkoutTime: new Date('20-July-2024'),
          rating: 4.999
        }
      ];
    
    
      fnName() {
    
      }
      toggle() {
        this.hideRoom = !this.hideRoom;
      }
    }
    

    html

    <div *ngIf="rooms.availableRooms > 0">
        Rooms List
        <!-- {{roomList}} -->
        <table>
            <tr>
                <th>Order</th>
                <th>Room Number</th>
                <th>Room Type</th>
                <th>Room Amenity</th>
                <th>Room Price</th>
                <th>Checkin Time</th>
                <th>Checkout Time</th>
                <th>Rating</th>
            </tr>
            <tr *ngFor="let room of roomList; let i=index;" [ngClass]="i%2 ? 'even' : 'odd'">
                <td>{{i}}</td>
                <td>{{room.roomNumber}}</td>
                <td>{{room.roomType}}</td>
                <td>{{room.amenities | lowercase}}</td>
                <td>{{room.price | currency : 'INR'}}</td>
                <td>{{room.checkinTime | date:'short'}}</td>
                <td>{{room.checkoutTime | date: 'short'}}</td>
                <td>{{room.rating | number}}</td>
            </tr>
        </table>
    </div>
    

    我尝试使用下面这种格式

    <td>{{room.rating | number: '1.1-2'}}</td>
    

    但出现了类似这样的错误

    X [ERROR] NG9: No overload matches this call.
      Overload 1 of 3, '(value: string | number, digitsInfo?: string | undefined, locale?: string | undefined): string | null', gave the following error.
        Argument of type 'Number' is not assignable to parameter of type 'string | number'.
      Overload 2 of 3, '(value: null | undefined, digitsInfo?: string | undefined, locale?: string | undefined): null', gave the following error.
        Argument of type 'Number' is not assignable to parameter of type 'null | undefined'.
      Overload 3 of 3, '(value: string | number | null | undefined, digitsInfo?: string | undefined, locale?: string | undefined): string | null', gave the following error.
        Argument of type 'Number' is not assignable to parameter of type 'string | number | null | undefined'. [plugin angular-compiler]
    
        src/app/rooms/rooms.component.html:30:23:
          30              <td>{{room.rating | number: '1.1-2'}}</td>
                                     ~~~~~~
    
      Error occurs in the template of component RoomsComponent.
    
        src/app/rooms/rooms.component.ts:9:15:
          9    templateUrl: './rooms.component.html',
                            ~~~~~~~~~~~~~~~~~~~~~~~~
    
返回
作者最近主题: