Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds Subset type for true subsets of interfaces #32407

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/lib.es5.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1432,6 +1432,11 @@ type Required<T> = {
[P in keyof T]-?: T[P];
};

/**
* Construct a true subset of T as defined by U
*/
type Subset<T extends U, U> = U;

/**
* Make all properties in T readonly
*/
Expand Down
93 changes: 93 additions & 0 deletions tests/baselines/reference/subsetTests.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
tests/cases/compiler/subsetTests.ts(16,28): error TS2304: Cannot find name 'Subset'.
tests/cases/compiler/subsetTests.ts(27,40): error TS2304: Cannot find name 'Subset'.
tests/cases/compiler/subsetTests.ts(32,34): error TS2304: Cannot find name 'Subset'.
tests/cases/compiler/subsetTests.ts(39,28): error TS2304: Cannot find name 'Subset'.
tests/cases/compiler/subsetTests.ts(49,40): error TS2304: Cannot find name 'Subset'.
tests/cases/compiler/subsetTests.ts(62,34): error TS2304: Cannot find name 'Subset'.


==== tests/cases/compiler/subsetTests.ts (6 errors) ====
interface User {
name: string;
age: number;
contact: {
email: string;
phone: string;
address: {
street: string;
country: string;
zipcode: number;
}
};
password: string;
}

type PersonalInformation = Subset<User, {
~~~~~~
!!! error TS2304: Cannot find name 'Subset'.
name: string;
age: number;
}>; // Fine

const test: PersonalInformation = {
name: 'Hans',
age: 21,
password: 'string' // Error: password does not exist in type
};

type WronglyTypedPersonalInformation = Subset<User, {
~~~~~~
!!! error TS2304: Cannot find name 'Subset'.
name: string;
age: string; // Error: Types of property age are incompatible
}>;

type ExcessPersonalInformation = Subset<User, {
~~~~~~
!!! error TS2304: Cannot find name 'Subset'.
name: string;
favoriteColor: string; // Error: Property favoriteColor is missing in type User
}>;

// This also works for "deep" properties

type ShippingInformation = Subset<User, {
~~~~~~
!!! error TS2304: Cannot find name 'Subset'.
name: string;
contact: {
address: {
street: string;
zipcode: number;
}
}
}>; // Fine (Omitting properties of nested properties is ok too)

type WronglyTypedShippingInformation = Subset<User, {
~~~~~~
!!! error TS2304: Cannot find name 'Subset'.
name: string;
contact: {
address: {
street: {
name: string;
nr: number;
}; // Error: Types of property street are incompatible
zipcode: number;
}
}
}>;

type ExcessShippingInformation = Subset<User, {
~~~~~~
!!! error TS2304: Cannot find name 'Subset'.
name: string;
contact: {
address: {
street: string;
zipcode: number;
state: string; // Error: Property state is missing in type User
}
}
}>;

80 changes: 80 additions & 0 deletions tests/baselines/reference/subsetTests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
//// [subsetTests.ts]
interface User {
name: string;
age: number;
contact: {
email: string;
phone: string;
address: {
street: string;
country: string;
zipcode: number;
}
};
password: string;
}

type PersonalInformation = Subset<User, {
name: string;
age: number;
}>; // Fine

const test: PersonalInformation = {
name: 'Hans',
age: 21,
password: 'string' // Error: password does not exist in type
};

type WronglyTypedPersonalInformation = Subset<User, {
name: string;
age: string; // Error: Types of property age are incompatible
}>;

type ExcessPersonalInformation = Subset<User, {
name: string;
favoriteColor: string; // Error: Property favoriteColor is missing in type User
}>;

// This also works for "deep" properties

type ShippingInformation = Subset<User, {
name: string;
contact: {
address: {
street: string;
zipcode: number;
}
}
}>; // Fine (Omitting properties of nested properties is ok too)

type WronglyTypedShippingInformation = Subset<User, {
name: string;
contact: {
address: {
street: {
name: string;
nr: number;
}; // Error: Types of property street are incompatible
zipcode: number;
}
}
}>;

type ExcessShippingInformation = Subset<User, {
name: string;
contact: {
address: {
street: string;
zipcode: number;
state: string; // Error: Property state is missing in type User
}
}
}>;


//// [subsetTests.js]
var test = {
name: 'Hans',
age: 21,
password: 'string' // Error: password does not exist in type
};
165 changes: 165 additions & 0 deletions tests/baselines/reference/subsetTests.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
=== tests/cases/compiler/subsetTests.ts ===
interface User {
>User : Symbol(User, Decl(subsetTests.ts, 0, 0))

name: string;
>name : Symbol(User.name, Decl(subsetTests.ts, 0, 16))

age: number;
>age : Symbol(User.age, Decl(subsetTests.ts, 1, 17))

contact: {
>contact : Symbol(User.contact, Decl(subsetTests.ts, 2, 16))

email: string;
>email : Symbol(email, Decl(subsetTests.ts, 3, 14))

phone: string;
>phone : Symbol(phone, Decl(subsetTests.ts, 4, 22))

address: {
>address : Symbol(address, Decl(subsetTests.ts, 5, 22))

street: string;
>street : Symbol(street, Decl(subsetTests.ts, 6, 18))

country: string;
>country : Symbol(country, Decl(subsetTests.ts, 7, 27))

zipcode: number;
>zipcode : Symbol(zipcode, Decl(subsetTests.ts, 8, 28))
}
};
password: string;
>password : Symbol(User.password, Decl(subsetTests.ts, 11, 6))
}

type PersonalInformation = Subset<User, {
>PersonalInformation : Symbol(PersonalInformation, Decl(subsetTests.ts, 13, 1))
>User : Symbol(User, Decl(subsetTests.ts, 0, 0))

name: string;
>name : Symbol(name, Decl(subsetTests.ts, 15, 41))

age: number;
>age : Symbol(age, Decl(subsetTests.ts, 16, 17))

}>; // Fine

const test: PersonalInformation = {
>test : Symbol(test, Decl(subsetTests.ts, 20, 5))
>PersonalInformation : Symbol(PersonalInformation, Decl(subsetTests.ts, 13, 1))

name: 'Hans',
>name : Symbol(name, Decl(subsetTests.ts, 20, 35))

age: 21,
>age : Symbol(age, Decl(subsetTests.ts, 21, 17))

password: 'string' // Error: password does not exist in type
>password : Symbol(password, Decl(subsetTests.ts, 22, 12))

};

type WronglyTypedPersonalInformation = Subset<User, {
>WronglyTypedPersonalInformation : Symbol(WronglyTypedPersonalInformation, Decl(subsetTests.ts, 24, 2))
>User : Symbol(User, Decl(subsetTests.ts, 0, 0))

name: string;
>name : Symbol(name, Decl(subsetTests.ts, 26, 53))

age: string; // Error: Types of property age are incompatible
>age : Symbol(age, Decl(subsetTests.ts, 27, 17))

}>;

type ExcessPersonalInformation = Subset<User, {
>ExcessPersonalInformation : Symbol(ExcessPersonalInformation, Decl(subsetTests.ts, 29, 3))
>User : Symbol(User, Decl(subsetTests.ts, 0, 0))

name: string;
>name : Symbol(name, Decl(subsetTests.ts, 31, 47))

favoriteColor: string; // Error: Property favoriteColor is missing in type User
>favoriteColor : Symbol(favoriteColor, Decl(subsetTests.ts, 32, 17))

}>;

// This also works for "deep" properties

type ShippingInformation = Subset<User, {
>ShippingInformation : Symbol(ShippingInformation, Decl(subsetTests.ts, 34, 3))
>User : Symbol(User, Decl(subsetTests.ts, 0, 0))

name: string;
>name : Symbol(name, Decl(subsetTests.ts, 38, 41))

contact: {
>contact : Symbol(contact, Decl(subsetTests.ts, 39, 17))

address: {
>address : Symbol(address, Decl(subsetTests.ts, 40, 14))

street: string;
>street : Symbol(street, Decl(subsetTests.ts, 41, 18))

zipcode: number;
>zipcode : Symbol(zipcode, Decl(subsetTests.ts, 42, 27))
}
}
}>; // Fine (Omitting properties of nested properties is ok too)

type WronglyTypedShippingInformation = Subset<User, {
>WronglyTypedShippingInformation : Symbol(WronglyTypedShippingInformation, Decl(subsetTests.ts, 46, 3))
>User : Symbol(User, Decl(subsetTests.ts, 0, 0))

name: string;
>name : Symbol(name, Decl(subsetTests.ts, 48, 53))

contact: {
>contact : Symbol(contact, Decl(subsetTests.ts, 49, 17))

address: {
>address : Symbol(address, Decl(subsetTests.ts, 50, 14))

street: {
>street : Symbol(street, Decl(subsetTests.ts, 51, 18))

name: string;
>name : Symbol(name, Decl(subsetTests.ts, 52, 21))

nr: number;
>nr : Symbol(nr, Decl(subsetTests.ts, 53, 29))

}; // Error: Types of property street are incompatible
zipcode: number;
>zipcode : Symbol(zipcode, Decl(subsetTests.ts, 55, 14))
}
}
}>;

type ExcessShippingInformation = Subset<User, {
>ExcessShippingInformation : Symbol(ExcessShippingInformation, Decl(subsetTests.ts, 59, 3))
>User : Symbol(User, Decl(subsetTests.ts, 0, 0))

name: string;
>name : Symbol(name, Decl(subsetTests.ts, 61, 47))

contact: {
>contact : Symbol(contact, Decl(subsetTests.ts, 62, 17))

address: {
>address : Symbol(address, Decl(subsetTests.ts, 63, 14))

street: string;
>street : Symbol(street, Decl(subsetTests.ts, 64, 18))

zipcode: number;
>zipcode : Symbol(zipcode, Decl(subsetTests.ts, 65, 27))

state: string; // Error: Property state is missing in type User
>state : Symbol(state, Decl(subsetTests.ts, 66, 28))
}
}
}>;

Loading