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

Different behavior between const and readonly for multidimensional array access/strict null checking #58476

Closed
delosford opened this issue May 8, 2024 · 7 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@delosford
Copy link

delosford commented May 8, 2024

🔎 Search Terms

different behavior between readonly and const

🕗 Version & Regression Information

  • I was unable to test this on prior versions because I'm using TS Playground
  • update: I tested it back a few versions after I was told how to on TS Playground and it produces the same errors.

⏯ Playground Link

No response

💻 Code

//this gives an error about possible null values
let someArray: Array<Array<Array<number | null> | null> | null> = new Array<Array<Array<number | null> | null> | null>();
let someInt = 0;

if (someArray[someInt] !== null && someArray[someInt][someInt] != null)
 console.log(someInt);

//this gives no error about possible null values, meaning the problem seems to be that someInt could theoretically be modified
let someArray: Array<Array<Array<number | null> | null> | null> = new Array<Array<Array<number | null> | null> | null>();
const someInt = 0;

if (someArray[someInt] !== null && someArray[someInt][someInt] != null)
 console.log(someInt);

//this however gives an error, with or without readonly

function itsaFunction(arrayAccessors: readonly number[])
{
	let someArray: Array<Array<Array<number | null> | null> | null> = new Array<Array<Array<number | null> | null> | null>();

	if (someArray[arrayAccessors[0]] !== null && someArray[arrayAccessors[0]][arrayAccessors[1]] != null)
 		console.log(arrayAccessors);
}

🙁 Actual behavior

Errors about possible null values do not treat const and readonly the same

🙂 Expected behavior

const and readonly should produce/fix the same errors in the same way

Additional information about the issue

No response

@MartinJohns
Copy link
Contributor

  • I was unable to test this on prior versions because I'm using TS Playground

Especially on the playground it's easy to test this on prior versions because the playground has a version selector on the top left.

@delosford
Copy link
Author

  • I was unable to test this on prior versions because I'm using TS Playground

Especially on the playground it's easy to test this on prior versions because the playground has a version selector on the top left.

I was unaware of that. Thanks for letting me know. I tested it back a few versions and they all produce the same errors.

@ahejlsberg
Copy link
Member

ahejlsberg commented May 8, 2024

This is working as intended. A readonly reference doesn't guarantee that the underlying object isn't mutated. I just guarantees that it isn't mutated through that reference. You need to store the value of arrayAccessors[0] in a const local to get the desired behavior.

@ahejlsberg ahejlsberg added the Working as Intended The behavior described is the intended behavior; this is not a bug label May 8, 2024
@delosford
Copy link
Author

function itsaFunction(arrayAccessors: readonly number[])
{
	let someArray: Array<Array<Array<number | null> | null> | null> = new Array<Array<Array<number | null> | null> | null>();

	const arrAcc1 = arrayAccessors[0];
	const arrAcc2 = arrayAccessors[1];

	if (someArray[arrAcc1] !== null && someArray[arrAcc1][arrAcc2] != null)
 		console.log(arrayAccessors);
}

Strangely, this doesn't work, either. Gives the same error.

@whzx5byb
Copy link

whzx5byb commented May 9, 2024

Strangely, this doesn't work, either.

This works in 5.5 beta. For more information, see https://devblogs.microsoft.com/typescript/announcing-typescript-5-5-beta/#control-flow-narrowing-for-constant-indexed-accesses

@delosford
Copy link
Author

Strangely, this doesn't work, either.

This works in 5.5 beta. For more information, see https://devblogs.microsoft.com/typescript/announcing-typescript-5-5-beta/#control-flow-narrowing-for-constant-indexed-accesses

Thanks for the info. Good to know this is at least being addressed.

@typescript-bot
Copy link
Collaborator

This issue has been marked as "Working as Intended" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale May 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

5 participants